65.9K
CodeProject is changing. Read more.
Home

To check string is palindrome or not in .NET (C#)

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.92/5 (8 votes)

Feb 3, 2011

CPOL
viewsIcon

150824

downloadIcon

79

It's very easy to check string is palindrome or not. Be sure efficiency is good or not.

I read some code for this, then some article and book using loop or other methods. But when I need this code for my practical examination, then I wrote this code. It is so easy.

Actually, there are some other ways also to check palindrome. So, be careful which code gives better efficiency. You can comment the line:

bool caseignore= str.Equals(revstr, StringComparison.OrdinalIgnoreCase);

and in if condition, use-

if(string.Compare(str,revstr,true)==0)
using System;
namespace palindromecheck
{
    class Program
    {
        static void Main(string[] args)
        {
            string str, revstr;
            Console.WriteLine("Enter Any String to Know It is Palindrome or not");
            str = Console.ReadLine();
            char[] tempstr = str.ToCharArray();
            Array.Reverse(tempstr);
            revstr = new string(tempstr);
            bool caseignore = str.Equals(revstr, StringComparison.OrdinalIgnoreCase);
            if (caseignore == true)
            {
                Console.WriteLine("............" + str + " Is a Palindrome..........");
            }
            else
            {
                Console.WriteLine("............" + str + " Is Not a Palindrome........");
            }
            Console.Read();
        }
    }
}