Palindromes

Dictionary meaning: a word, line, verse, number, sentence, etc., reading the same backward as forward, as Madam 

Palindrome in the C# language: A palindrome has the same letters on both ends of the string. This means “rotator” is a palindrome, but “advice” is not.

The simplest concept to test a string as a palindrome is to check if it’s the same reversed as it was originally. Thus our first C# palindrome function will reverse the entire string and compare it to the original.

Code Sample:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IsPalindrome
{
    class Program
    {
        static void Main(string[] args)
        {
            string reversed = string.Empty, userInput = string.Empty;
            Console.WriteLine("Enter a String to check for Palindrome:");
            userInput = Console.ReadLine(); //Get Userinput

            int iLength = userInput.Length; // Get Length of string
            if (userInput.Length > 0)
            {
                //Reverse String ----
                for (int j = iLength - 1; j >= 0; j--)
                {
                    reversed = reversed + userInput[j];
                }

                //Check with original
                if (reversed == userInput)
                    Console.WriteLine(userInput + " is palindrome");
                else
                    Console.WriteLine(userInput + " is not a palindrome");

                Console.WriteLine("Original Input is = {0}", userInput);
                Console.WriteLine("Reversed Input is  = {0}", reversed);
                Console.Read();
            }
            else
            {
                Console.WriteLine("Please Enter String to Check!!");
            }
        }
    }
}

Out Put:
Enter a String to check for Palindrome:
Shashangka
Shashangka is not a palindrome
Original Input is = Shashangka
Reversed Input is = akgnahsahS

Author:

Since March 2011, have 8+ years of professional experience on software development, currently working as Senior Software Engineer at s3 Innovate Pte Ltd.

Leave a Reply