This code could be better?
This is the algorithm:
- Compare the 1st character to the last character
- Compare the 2nd character to the second last character and so on
- Stop when the middle of the string is reached
- Just words
namespace Palindrome
{
class Class1
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("Digit a word:");
string word = Console.ReadLine();
if (word == "exit")
break;
VerifyPalindrome(word);
}
}
private static void VerifyPalindrome(string word)
{
bool compare = true;
int i = 0;
while (i < word.Length- 1)
{
if (compare)
{
for (int j = word.Length - 1; j >= 0; j--)
{
if (word[i] == word[j])
compare = true;
else
{
compare = false;
break;
}
i++;
}
}
else
break;
}
if (compare)
Console.WriteLine("This is a Palindrome word");
else
Console.WriteLine("This is NOT a Palindrome word");
}
}
}
fooandbaare palindromes. \$\endgroup\$while (i < (word.Length / 2) - 1)Thanks \$\endgroup\$