Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

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:

Popular

Tags

Mobile

Recent Posts