Introduction
In this article, we are going to explore how to determine whether a string is a Palindrome or not.
This is an essential technical interview question that may be posed to beginner, intermediate, and experienced candidates.
We previously covered how to reverse a string in the last article,
- How to Reverse Number in C#
- How to Reverse a String in C#
We will be discussed,
- What is a Palindrome string?
- How to check whether a string is Palindrome or not?
Let’s understand,
What is a Palindrome String?
In simple words, the string will remain the same when reading from both sides. Let's see the below example of a Palindrome string,
Example of the Palindrome Strings
- Madam
- Racecar
- Civic
- Radar etc
I hope you comprehended what constitutes a palindrome string. Now, we will create a C# example to accomplish this,
How to check whether a string is Palindrome or not?
First create Asp.Net Core Console application and write the below code in the program.cs file,
Console.WriteLine("Enter a string");
var str = Console.ReadLine();
var reverseNumber = new String(str.Reverse().ToArray());
if (str.Equals(reverseNumber))
{
Console.WriteLine($"{str} is a palindrome string");
}
else
{
Console.WriteLine($"{str} is NOT a palindrome string");
}
Console.ReadLine();
Now, we will run the application and observe the output,
Madam is a palindrome string. Nice.
Run again with the different input and see the output.
We got the correct output.
That’s all for this article, I Hope you enjoyed and learn something new.