• C# Program to Check Whether a string is palindrome or not in c sharp .NET


    Palindrome-

    A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. see more at wikipedia

    It's very easy to check string is palindrome or not. There are lots of ways to check palindrome (Like- using loop). So, be careful which code gives better efficiency. 

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

    For more simplification, We can remove the line-
    bool caseignore= str.Equals(revstr, StringComparison.OrdinalIgnoreCase);
    and replace the if condition with-
    if(string.Compare(str,revstr,true)==0)



  • You might also like

    No comments:

    Post a Comment

Powered by Blogger.