How to check Palindrome word using C#

Palindrome word can be read in both direction. Suppose “level” can be read in both direction, so it is palindrome word. To check palindrome word you can try like following code using C#.


    class Program
    {
        public static bool IsPalindrome(string word)
        {
            int minLength = 0;
            int maxLength = word.Length - 1;
            while (true)
            {
                if (minLength > maxLength)
                {
                    return true;
                }
                char a = word[minLength];
                char b = word[maxLength];
                if (char.ToLower(a) != char.ToLower(b))
                {
                    return false;
                }
                minLength++;
                maxLength--;
            }
        }

        static void Main()
        {
            string[] palindromeWord = {"aibohphobia","Dhaka","alula","cammac","Civic", "deified","deleveled",
                                 "detartrated","devoved","History","evitative","level","","mahedee"};

            foreach (string word in palindromeWord)
            {
                if (IsPalindrome(word))
                {
                    Console.WriteLine(word + " is a Palindrome");
                }
                else
                {
                    Console.WriteLine(word + " is not a Palindrome");
                }
            }
            Console.ReadKey();
        }
    }

Please share, if it is helpfulShare on FacebookShare on LinkedInTweet about this on TwitterShare on Google+Email this to someonePrint this page

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>