Saturday, November 25, 2006

String is a Palindrome !

How to check if a string is a palindrome ?
You can change the code a bit to reverse an array


public static bool IsPalindrome(string s)
{
int length =s.Length;
char[] chrArray = s.ToCharArray();
if (length == 0)
return false;
if (length == 1)
return true;

int start = 0;
int end = length - 1;

while (end > start)
{
if (chrArray[start] == chrArray[end])
{
start++;
end--;
}
else
{
return false;
}
}
return true;
}