Software & Finance





C# - Palindrome of a String





I have given here C# program to check whether given string is a palindrome or not

 

 

Source Code


using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

 

          static bool IsPalindrome(string src)

        {

            bool palindrome = true;

            for (int i = 0; i < src.Length / 2 + 1; i++)

            {

                if (src[i] != src[src.Length - i - 1])

                {

                    palindrome = false;

                    break;

                }

            }

            return palindrome;

        }

 

          static void Main(string[] args)

        {

            Console.Write("Enter a String: ");

            string s = Console.ReadLine();

            if (IsPalindrome(s) == true)

            {

                Console.WriteLine(s + " is a palindrome");

            }

            else

            {

                Console.WriteLine(s + " is NOT a palindrome");

            }

        }

    }

}

Output


Enter a String: malayalam

malayalam is a palindrome

 

Press any key to continue . . .

 

 

Enter a String: Kathir

Kathir is NOT a palindrome

 

Press any key to continue . . .