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"
1 Respuesta
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;
}
}