Palindrome String Program in C#

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,

  1. How to Reverse Number in C# 
  2. How to Reverse a String in C# 

We will be discussed,

  1. What is a Palindrome string?
  2. 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

  1. Madam
  2. Racecar
  3. Civic
  4. 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,

Palindrome String

Madam is a palindrome string. Nice.

Run again with the different input and see the output.

Palindrome String

We got the correct output.

That’s all for this article, I Hope you enjoyed and learn something new.


Similar Articles