Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now

Palindrome in C#


Palindrome.
 Palindromes can be read in both directions. A palindrome has the same letters on both ends of the string. It is a form of word puzzle.
using System;

namespace Palindrome
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please give a word which needs to be reverce \n");
         string x = Console.ReadLine();
         string ReverceString =   Reverse(ref x);
         Console.WriteLine("Reverce String is =" + ReverceString);
         if (IsPalindromeforWord(x))
             Console.WriteLine("Word is a palindrome \n");
            else
             Console.WriteLine("Word is not a palindrome \n");

         Console.WriteLine("Please give a Sentance \n");

         string Sentance = Console.ReadLine();

         if (IsPalindromeforSentance(Sentance))
             Console.WriteLine("Sentance is a palindrome ");
            else
             Console.WriteLine("Sentance is not a palindrome ");


         Console.ReadLine();
        }

        public static string Reverse(ref string x)
        {
            string result = "";
            for (int i = x.Length - 1; i >= 0; i--)
                result += x[i];
            return result;//Was it a car or a cat I saw
        }

        public static bool IsPalindromeforWord(string word)
        {
            int min = 0;
            int max = word.Length - 1;
            while (true)
            {
                if (min > max)
                {
                    return true;
                }
                char a = word[min];
                char b = word[max];
                if (char.ToLower(a) != char.ToLower(b))
                {
                    return false;
                }
                min++;
                max--;
            }
        }

        // Function for Palindrome a Sentance
        public static bool IsPalindromeForSentance(string value)
        {
            int min = 0;
            int max = value.Length - 1;
            while (true)
            {
                if (min > max)
                {
                    return true;
                }
                char a = value[min];
                char b = value[max];

                // Scan forward for a while invalid.
                while (!char.IsLetterOrDigit(a))
                {
                    min++;
                    if (min > max)
                    {
                        return true;
                    }
                    a = value[min];
                }

                // Scan backward for b while invalid.
                while (!char.IsLetterOrDigit(b))
                {
                    max--;
                    if (min > max)
                    {
                        return true;
                    }
                    b = value[max];
                }

                if (char.ToLower(a) != char.ToLower(b))
                {
                    return false;
                }
                min++;
                max--;
            }
        }
    }
}


Share this

Related Posts

Previous
Next Post »

Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now