Sunday, May 19, 2024
HomeC#Program to verify a string is Palindrome in C# ~ IT Tutorials...

Program to verify a string is Palindrome in C# ~ IT Tutorials with Instance


On this weblog, we are going to be taught Methods to write a program to verify whether or not a String is a Palindrome or not in C#. A palindrome is a phrase or phrase or a sequence of characters that reads the identical both learn from backward or as in forwards e.g. Malayalam, Madam, and so forth. 

It is usually one of many in style and favourite applications requested through the interview/examination. There are a number of methods by which we will verify both string is palindrome or not, Out of which two are talked about right here on this weblog.

1. Iterate via every character and generate the reverse string

2. Break string to array and reverse the string

1. Iterate via every character and generate reverse string:

On this methodology, we begin studying the character from the final character of the string and retailer it in one other variable as proven within the under picture. As soon as the string is reversed, examine it with the unique string to search out that string entered by the person is Palindrome or not.

Program in C#:

Console.WriteLine(“Enter
a string to verify both it is Palindrome or not”
);

string assertion = Console.ReadLine();

//variable to carry the reversed assertion

string reverseStatement = “”;

//Reverse the assertion

for (int i=assertion.Size-1;i>=0;i–) {

    reverseStatement +=
assertion[i];

}

//Test assertion and reverse assertion are equal

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

{

    Console.WriteLine(“‘{0}’ is an instance of Palindrome”, assertion);

}

else {

    Console.WriteLine(“‘{0}’ shouldn’t be an instance of Palindrome”, assertion);

}

//Console.ReadLine() to carry the display screen after the execution of
this system

Console.ReadLine();

Output:

2. Break string to array and reverse the string

One other and best approach to verify the palindrome is to transform the string to the char array after which reverse the array with Array.Reverse() methodology. As soon as the array is reversed, convert it again to the string as proven within the under instance. As soon as the string is reversed, examine it (ignore case whereas evaluating string) with the unique string entered by the person.

Program in C#:

Console.WriteLine(“Enter
a string to verify both it is Palindrome or not”
);

string assertion = Console.ReadLine();

//variable to carry the reversed assertion

string reverseStatement = “”;

//Convert the string to the Char Array

Char[] charArr = assertion.ToCharArray();

//Reverse the Array

Array.Reverse(charArr);

//Convert the character array to string

reverseStatement = new string(charArr);

//Test assertion and reverse assertion are equal

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

{

    Console.WriteLine(“‘{0}’ is an instance of Palindrome”, assertion);

}

else

{

    Console.WriteLine(“‘{0}’ shouldn’t be an instance of Palindrome”, assertion);

}

//Console.ReadLine() to carry the display screen after the execution of
this system

Console.ReadLine();

Preview:

I hope this program will allow you to in your interview/examination.

Thanks

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments