Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Sunday, 13 September 2020

Program to check a string is Palindrome in C#

In this blog, we will learn How to write a program to check whether a String is a Palindrome or not in C#. A palindrome is a word or phrase or a sequence of characters that reads the same either read from backward or as in forwards e.g. Malayalam, Madam, etc. 

It is also one of the popular and favorite programs asked during the interview/exam. There are multiple ways by which we can check either string is palindrome or not, Out of which two are mentioned here in this blog.
1. Iterate through each character and generate the reverse string
2. Break string to array and reverse the string

1. Iterate through each character and generate reverse string:
In this method, we start reading the character from the last character of the string and store it in another variable as shown in the below image. Once the string is reversed, compare it with the original string to find that string entered by the user is Palindrome or not.
Program in C#:

Console.WriteLine("Enter a string to check either it's Palindrome or not");

string statement = Console.ReadLine();

//variable to hold the reversed statement

string reverseStatement = "";

//Reverse the statement

for (int i=statement.Length-1;i>=0;i--) {

    reverseStatement += statement[i];

}

//Check statement and reverse statement are equal

if (statement.Equals(reverseStatement, StringComparison.OrdinalIgnoreCase))

{

    Console.WriteLine("'{0}' is an example of Palindrome", statement);

}

else {

    Console.WriteLine("'{0}' is not an example of Palindrome", statement);

}

//Console.ReadLine() to hold the screen after the execution of the program

Console.ReadLine();

Output:


2. Break string to array and reverse the string
Another and easiest way to check the palindrome is to convert the string to the char array and then reverse the array with Array.Reverse() method. Once the array is reversed, convert it back to the string as shown in the below example. Once the string is reversed, compare it (ignore case while comparing string) with the original string entered by the user.
Program in C#:

Console.WriteLine("Enter a string to check either it's Palindrome or not");

string statement = Console.ReadLine();

//variable to hold the reversed statement

string reverseStatement = "";

//Convert the string to the Char Array

Char[] charArr = statement.ToCharArray();

//Reverse the Array

Array.Reverse(charArr);

//Convert the character array to string

reverseStatement = new string(charArr);

//Check statement and reverse statement are equal

if (statement.Equals(reverseStatement, StringComparison.OrdinalIgnoreCase))

{

    Console.WriteLine("'{0}' is an example of Palindrome", statement);

}

else

{

    Console.WriteLine("'{0}' is not an example of Palindrome", statement);

}

//Console.ReadLine() to hold the screen after the execution of the program

Console.ReadLine();

Preview:

I hope this program will help you in your interview/exam.
Thanks


0 comments:

Post a Comment

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook

Blog Archive