Checking for a palindrome string is similar to how we check for a palindrome number, except that we go with full-length character operations instead of modulo divisions.
public static bool IsPalindromeString(string input)
{
var reverse = new char[input.Length];
for (int j = 0, i = input.Length - 1; i >= 0; j++, i--)
{
reverse[j] = input[i];
}
for (int i = 0; i < input.Length; i++)
{
if (input[i] != reverse[i])
{
return false;
}
}
return true;
}
How it works:
We begin by copying the input string in reverse into another character array, and then examine both the strings character by character for any inequality. If none found, then its a palindrome else its not.
Output:
Enter input: abcdcba
abcdcba is Palindrome? : True
Enter input: abcabc
abcabc is Palindrome? : False