Click here to Skip to main content
11,492,639 members (61,202 online)
Click here to Skip to main content

Tagged as

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

, 6 Feb 2011 CPOL 62.2K 79 5
Rate this:
Please Sign up or sign in to vote.
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();
        }
    }
}

License

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

Share

About the Author

ashishkumar008
Student ORIENTAL INSTITUTE OF SCIENCE AND TECHNOLOGY, RGPV
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pin
Sibeesh Venu2-Aug-14 21:48
professionalSibeesh Venu2-Aug-14 21:48 
QuestionAlternative Solutions [modified] Pin
DARSHAN MODANI3-Mar-12 12:20
memberDARSHAN MODANI3-Mar-12 12:20 
GeneralReason for my vote of 5 its nice one Pin
Pranay Rana3-Feb-11 20:22
memberPranay Rana3-Feb-11 20:22 
GeneralReason for my vote of 5. I like it Pin
RaviRanjankr3-Feb-11 20:14
mvpRaviRanjankr3-Feb-11 20:14 
GeneralReason for my vote of 5 My vote of 5 reason I like it Pin
RaviRanjankr3-Feb-11 20:13
mvpRaviRanjankr3-Feb-11 20:13 
GeneralVery nice Pin
Umair Feroze3-Feb-11 6:00
memberUmair Feroze3-Feb-11 6:00 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 7 Feb 2011
Article Copyright 2011 by ashishkumar008
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid