C# program to check if a string is palindrome or not


To check if a string is palindrome or not, you need to first find the reverse of the string using −

Array.reverse()

After that use the equals() method to match the original string with the reversed. If the result is true, that would mean the string is Palindrome.

Example

Let us try the complete example. Here, our string is “Malayalam”, which is when reversed gives the same result.

Live Demo

using System;
namespace palindromecheck {
   class Program {
      static void Main(string[] args) {
         string string1, rev;
         string1 = "Malayalam";
         char[] ch = string1.ToCharArray();
         Array.Reverse(ch);
         rev = new string(ch);
         bool b = string1.Equals(rev, StringComparison.OrdinalIgnoreCase);
         if (b == true) {
            Console.WriteLine("" + string1 + " is a Palindrome!");
         } else {
            Console.WriteLine(" " + string1 + " is not a Palindrome!");
         }
         Console.Read();
      }
   }
}

Output

Malayalam is a Palindrome!

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 19-Jun-2020

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements