Check if number/string is palindrome in C#

A palindrome is a word, phrase, or sequence of characters that reads the same backward as forward. In C#, there are several ways to write a program to check whether a given string is a palindrome or not. Here are four possible methods:

Using a loop

One way to check if a string is a palindrome in C# is to use a loop to iterate through the characters of the string and compare the first and last characters, the second and second to last characters, and so on.

using System; public class Palindrome { public static bool IsPalindrome(string str) { for (int i = 0; i < str.Length / 2; i++) { if (str[i] != str[str.Length - i - 1]) { return false; } } return true; } public static void Main(string[] args) { string str = "radar"; bool isPalindrome = IsPalindrome(str); Console.WriteLine(isPalindrome); } }
//Output: True

This method defines a static function called IsPalindrome() that takes a string as its argument and returns true if the string is a palindrome and false otherwise. It uses a for loop to iterate through the characters of the string up to its halfway point. It compares the first character to the last character, the second character to the second to last character, and so on. If any pair of characters is not equal, the function returns false. If all pairs of characters are equal, the function returns true.

Using recursion

Another way to check if a string is a palindrome in C# is to use recursion. This method is not very efficient, but it is an interesting example of how recursion can be used to solve simple problems.

using System; public class Palindrome { public static bool IsPalindrome(string str) { if (str.Length <= 1) { return true; } else { return (str[0] == str[str.Length - 1]) && IsPalindrome(str.Substring(1, str.Length - 2)); } } public static void Main(string[] args) { string str = "12321"; bool isPalindrome = IsPalindrome(str); Console.WriteLine(isPalindrome); } }
//Output: True

This method defines a static function called IsPalindrome() that takes a string as its argument and returns true if the string is a palindrome and false otherwise. If the length of the string is 1 or less, the function returns true. Otherwise, it checks whether the first and last characters of the string are equal, and then recursively calls itself with the substring of the original string that excludes the first and last characters. If all pairs of characters are equal, the function returns true.

Using LINQ


How to Palindrome program in C#

You can also use the LINQ (Language Integrated Query) library to check if a string is a palindrome in C#.

using System; public class Palindrome { public static bool IsPalindrome(string str) { return str.SequenceEqual(str.Reverse()); } public static void Main(string[] args) { string str = "radar"; bool isPalindrome = IsPalindrome(str); Console.WriteLine(isPalindrome); } }
//Output: True

This method defines a static function called IsPalindrome() that takes a string as its argument and returns true if the string is a palindrome and false otherwise. It uses the SequenceEqual() method provided by the LINQ library to compare the original string with a reversed version of the string. If the two sequences are equal, the function returns true.

Using string manipulation

You can check if a given string is a palindrome using string manipulation methods. Following is an example of how to write a palindrome program using string manipulation in C#:

using System; public class Palindrome { public static bool IsPalindrome(string str) { string reversedStr = string.Empty; // Reverse the input string for (int i = str.Length - 1; i >= 0; i--) { reversedStr += str[i]; } // Check if the reversed string is equal to the input string return str.Equals(reversedStr, StringComparison.OrdinalIgnoreCase); } public static void Main(string[] args) { string inputStr = "radar"; bool isPalindrome = IsPalindrome(inputStr); Console.WriteLine(isPalindrome); } }
//Output: True

This program defines a static function called IsPalindrome() that takes a string as its input and returns a boolean value indicating whether or not the input string is a palindrome. The function first initializes an empty string called reversedStr to store the reversed input string. Then, using a for loop, it iterates over the characters of the input string in reverse order and appends each character to reversedStr. Finally, it compares the input string with the reversed string using the Equals() method and the StringComparison.OrdinalIgnoreCase option, which performs a case-insensitive comparison of the two strings.

In the main method, the program calls IsPalindrome() with the string "radar" and stores the result in the boolean variable isPalindrome. The program then outputs the value of isPalindrome, which is true since "radar" is a palindrome.