Check if a given string is Palindrome

A Palindrome is defined as a phrase or a number which when reversed also gives the same phrase or number. For example, the word abcdcba is a palindrome because even when you read the string from end to the beginning you'll get the same string as the original. Some real words such as malayalam, madam, refer, reviver, noon, mom and such are all palindromes in general use.

Checking for a palindrome string is similar to how we check for a palindrome number, except that we go with full-length character operations instead of modulo divisions.


        public static bool IsPalindromeString(string input)
        {
            var reverse = new char[input.Length];

            for (int j = 0, i = input.Length - 1; i >= 0; j++, i--)
            {
                reverse[j] = input[i];
            }

            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] != reverse[i])
                {
                    return false;
                }
            }

            return true;
        }
        

How it works:

We begin by copying the input string in reverse into another character array, and then examine both the strings character by character for any inequality. If none found, then its a palindrome else its not.

Output:

Enter input: abcdcba
abcdcba is Palindrome? : True

Enter input: abcabc
abcabc is Palindrome? : False
Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Privacy Overview
Referbruv

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

3rd Party Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.