How do you check if a given string is a palindrome?





How do you check if a given string is a palindrome?

Example  

Input : madam
Output : palindrome

Input : madam
Output : Not a palindrom

Solution:

Step’s
·         First do some validation
·         Store the string in a char array
·         Using loop store reverse string in another string
·         Compare the both string


public static void Palindrom(string str)
        {
            if ((str == null) || (str.Length <= 1))
                Console.Write(str);

            else
            {
                str = str.ToUpper();
                char[] chr = str.ToCharArray();
                string str2 = "";
                for (int i = chr.Length - 1; i >= 0; i--)
                {
                    str2 += chr[i];
                }
                if (str.Equals(str2))
                {
                    Console.Write("String " + str + " is palindrom ");
                }
                else
                {
                    Console.Write("String " + str + " is Not a palindrom ");
                }
               
            }

        }


static void Main(string[] args)
        {
            Console.WriteLine("=============================================================================");
            Console.WriteLine("How do you check if a given string is a palindrome?");
            Console.WriteLine("=============================================================================");
            Program objprog = new Program();
            Program.Palindrom("madam");
            Console.ReadLine();


        }

Share:

1 comment:

  1. Shootercasino Casino
    Shootercasino casino offers slots machines, poker and live dealer games. Join the action 제왕 카지노 with Playtech, and 바카라 사이트 get a £10 Welcome Bonus. septcasino Exclusive

    ReplyDelete

Popular

Tags

Mobile

Recent Posts