Click here to Skip to main content
15,886,919 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.92/5 (8 votes)
6 Feb 2011CPOL 149.2K   79   5   6
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:

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

and in if condition, use-


C#
if(string.Compare(str,revstr,true)==0)

C#
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();
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student ORIENTAL INSTITUTE OF SCIENCE AND TECHNOLOGY, RGPV
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Sibeesh Venu2-Aug-14 20:48
professionalSibeesh Venu2-Aug-14 20:48 
QuestionAlternative Solutions Pin
DARSHAN MODANI3-Mar-12 11:20
DARSHAN MODANI3-Mar-12 11:20 
GeneralReason for my vote of 5 its nice one Pin
Pranay Rana3-Feb-11 19:22
professionalPranay Rana3-Feb-11 19:22 
GeneralReason for my vote of 5. I like it Pin
RaviRanjanKr3-Feb-11 19:14
professionalRaviRanjanKr3-Feb-11 19:14 
GeneralReason for my vote of 5 My vote of 5 reason I like it Pin
RaviRanjanKr3-Feb-11 19:13
professionalRaviRanjanKr3-Feb-11 19:13 
GeneralVery nice Pin
Umair Feroze3-Feb-11 5:00
Umair Feroze3-Feb-11 5:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.