Here's the code to check if a string is a palindrome in C#:
main.cs
publicstaticboolIsPalindrome(string word){
// Convert the string to lowercase to ignore case sensitivity word = word.ToLower();
// Reverse the stringchar[] charArray = word.ToCharArray();
Array.Reverse(charArray);
string reversedWord = newstring(charArray);
// Compare the original word with the reversed wordif (word == reversedWord)
{
returntrue;
}
else {
returnfalse;
}
}
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.");
}