Site Loader
Auckland, New Zealand
This code checks if any given word is Palindrome Here is the algorithm
public static bool IsPalindrome(string word)
        {
            //Declare variables
            int start, mid;
            start = 0;
            string firsthalf = null, lasthalf = null;
            //Get mid of the value
            mid = (start + word.Length) / 2;
            //First half
            for (int i = start; i <= mid - 1; i++)
                firsthalf = firsthalf + word.Substring(i, 1);
            //Second half
            for (int j = word.Length - 1; j >= mid + 1; j--)
                lasthalf = lasthalf + word.Substring(j, 1);
            //Compare both half
            if (firsthalf == lasthalf)
            {
                Console.WriteLine(word + " is Palindrome");
                return true;
            }
            else
            {
                Console.WriteLine(word + " is not Palindrome");
                return false;
            }
        }


Thanks, Karthik KK

Post Author: Karthik kk

Leave a Reply

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