How do I check wether a string is a palindrom? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I check wether a string is a palindrom?

A palindrom is a word who reads forwards and backwards as the same. Example: "Otto"

21st Jul 2016, 11:07 PM
Idril
Idril - avatar
1 Answer
0
I use a string extension method like that: public static class StringCheckerExtensions { public static bool IsPalindrom ( this string str, bool caseSensitive = false ) { if (String.IsNullOrWhiteSpace(str)) throw new Exception("String must not be empty."); bool result = false; if(!caseSensitive) str = str.ToUpper(); int halfLen = str.Length / 2; for (int i = 0; i < halfLen; i++) { result = str[i] == str[(str.Length - 1) - i]; if (!result) break; } return result; } }
21st Jul 2016, 11:09 PM
Idril
Idril - avatar