check if string is palindrome in csharp

Here's the code to check if a string is a palindrome in C#:

main.cs
public static bool IsPalindrome(string word)
{
    // Convert the string to lowercase to ignore case sensitivity
    word = word.ToLower();

    // Reverse the string
    char[] charArray = word.ToCharArray();
    Array.Reverse(charArray);
    string reversedWord = new string(charArray);

    // Compare the original word with the reversed word
    if (word == reversedWord)
    {
        return true;
    }
    else
    {
        return false;
    }
}
454 chars
21 lines

You can then call the IsPalindrome method and pass in a string to check if it's a palindrome:

main.cs
string word = "racecar";
bool isPalindrome = IsPalindrome(word);

if (isPalindrome)
{
    Console.WriteLine("The word is a palindrome.");
}
else
{
    Console.WriteLine("The word is not a palindrome.");
}
205 chars
12 lines

gistlibby LogSnag