namespace Palindrome
{
class Program
{
public static bool IsPalindrome(string s)
{
int min = 0;
int max = s.Length - 1;
while (true)
{
if (min > max) // True if we've compared up to, and then gone passed the middle of the string.
return true;
if (char.ToLower(s[min++]) != char.ToLower(s[max]))
return false;
}
}
static void Main(string[] args)
{
string [] words = {
"civic",
"deified",
// ...
"stats",
"tenet",
};
foreach (string value in words)
{
Console.WriteLine("{0} = {1}", value, IsPalindrome(value));
}
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey(true); }
}
}
The program checks to see if the words in the words array are Palindromes (words spelled the same forwards as they are backwards).
The foreach
loop in Main passes each of the words in the array to the IsPalindrome()
function; which tests the word, and returns True or False accordingly.
As each word in the current array is a Palindrome, when the program is run, it should output all of the current words, followed by True. However, it gives me False. Why is that?