Program for Palindrome checking in C Sharp/C#

Hi,

Sometimes you need to check for whether the given string is Palindrome or not. Given below is a sample code to check whether the given string is Palindrome or not using C Sharp/C#.

class Palindrome
{
  static void Main()
  {
      string checkStr, sampleStr;
      char[] temp;

      System.Console.Write("Enter your String: ");

      sampleStr = System.Console.ReadLine();

      checkStr = sampleStr.Replace(" ", "");

      checkStr = checkStr.ToLower();

      temp = checkStr.ToCharArray();

      System.Array.Reverse(temp);

      if(checkStr== new string(temp))
      {
          System.Console.WriteLine(""{0}" is a palindrome.",sampleStr);
      }

      else
      {
          System.Console.WriteLine(""{0}" is NOT a palindrome.",sampleStr);
      }

  }
}

:)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>