Check a Palindrome String in C#

A palindrome string is the one that once reversed produces the same result as the original input string. Let us proceed with an example for the same.

Write the following code in a console application.

  1. static void Main(string[] args)  
  2. {  
  3.     string _inputstr, _reversestr = string.Empty;  
  4.     Console.Write("Enter a string : ");  
  5.     _inputstr = Console.ReadLine();  
  6.     if (_inputstr != null)  
  7.     {  
  8.         for (int i = _inputstr.Length - 1; i >= 0; i--)  
  9.         {  
  10.             _reversestr += _inputstr[i].ToString();  
  11.         }  
  12.         if (_reversestr == _inputstr)  
  13.         {  
  14.             Console.WriteLine("String is Palindrome Input = {0} and Output= {1}", _inputstr, _reversestr);  
  15.         }  
  16.         else  
  17.         {  
  18.             Console.WriteLine("String is not Palindrome Input = {0} and Output= {1}", _inputstr, _reversestr);  
  19.         }  
  20.     }  
  21.     Console.ReadLine();  
  22. }  
Let us check the output now. I would be using a palindrome string first to check if the logic is working properly. And in the second case a non palindrome string to verify the same. Since my name is palindrome so i would be using it to verify it first.

Output

Palindrome

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now