The Program in C# Program to Check If Given string is a palindrome or not is given below:
using System;
namespace PalindromeChecker
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family,enter a word or sentence to check if the number is palindrome or not!: ");
string input = Console.ReadLine();
// Remove non-alphanumeric characters and spaces
input = input.Replace(" ", string.Empty);
input = new string(Array.FindAll(input.ToCharArray(), (c => (char.IsLetterOrDigit(c)))));
// Convert the input to lowercase
input = input.ToLower();
// Check if the input is a palindrome
bool isPalindrome = true;
int length = input.Length;
for (int i = 0; i < length / 2; i++)
{
if (input[i] != input[length - i - 1])
{
isPalindrome = false;
break;
}
}
// Display the result
Console.WriteLine("Is the input a palindrome? " + isPalindrome);
Console.ReadKey();
}
}
}
Output:
Hello Codeauri Family,enter a word or sentence to check if the number is palindrome or not!:
yey
Is the input a palindrome? True
Pro-Tips💡
Here are the step by step execution of above program:
using System;
: This line is a using directive that specifies that the code uses theSystem
namespace. It allows us to access the classes and methods in theSystem
namespace without having to qualify it every time.namespace PalindromeChecker
: This line creates a new namespace namedPalindromeChecker
. A namespace is used to organize code and prevent naming collisions.class Program
: This line declares a new class namedProgram
. A class is a blueprint for creating objects.static void Main(string[] args)
: This line declares the entry point of the program. TheMain
method is where the program starts executing when you run the program.Console.WriteLine("Hello Codeauri Family,enter a word or sentence to check if the number is palindrome or not!: ");
: This line writes the message “Hello Codeauri Family,enter a word or sentence to check if the number is palindrome or not!” to the console.string input = Console.ReadLine();
: This line reads the input from the console and stores it in a variable namedinput
.input = input.Replace(" ", string.Empty);
: This line removes all spaces from theinput
string.input = new string(Array.FindAll(input.ToCharArray(), (c => (char.IsLetterOrDigit(c)))));
: This line converts theinput
string to a character array, removes all non-alphanumeric characters from it, and converts it back to a string.input = input.ToLower();
: This line converts theinput
string to lowercase.bool isPalindrome = true;
: This line declares a variable namedisPalindrome
and initializes it totrue
.int length = input.Length;
: This line gets the length of theinput
string and stores it in a variable namedlength
.for (int i = 0; i < length / 2; i++)
: This line starts a for loop that will iteratelength / 2
times.if (input[i] != input[length - i - 1])
: This line checks if the character at indexi
in theinput
string is not equal to the character at indexlength - i - 1
in theinput
string.isPalindrome = false;
: If the characters are not equal, theisPalindrome
variable is set tofalse
.break;
: If the characters are not equal, the loop is exited.Console.WriteLine("Is the input a palindrome? " + isPalindrome);
: This line writes the message “Is the input a palindrome?” followed by the value of theisPalindrome
variable to the console.Console.ReadKey();
: This line waits for the user to press a key.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.