Basic Programming C# Examples

C# Program to Check If Given string is a palindrome or not

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:

  1. using System;: This line is a using directive that specifies that the code uses the System namespace. It allows us to access the classes and methods in the System namespace without having to qualify it every time.
  2. namespace PalindromeChecker: This line creates a new namespace named PalindromeChecker. A namespace is used to organize code and prevent naming collisions.
  3. class Program: This line declares a new class named Program. A class is a blueprint for creating objects.
  4. static void Main(string[] args): This line declares the entry point of the program. The Main method is where the program starts executing when you run the program.
  5. 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.
  6. string input = Console.ReadLine();: This line reads the input from the console and stores it in a variable named input.
  7. input = input.Replace(" ", string.Empty);: This line removes all spaces from the input string.
  8. input = new string(Array.FindAll(input.ToCharArray(), (c => (char.IsLetterOrDigit(c)))));: This line converts the input string to a character array, removes all non-alphanumeric characters from it, and converts it back to a string.
  9. input = input.ToLower();: This line converts the input string to lowercase.
  10. bool isPalindrome = true;: This line declares a variable named isPalindrome and initializes it to true.
  11. int length = input.Length;: This line gets the length of the input string and stores it in a variable named length.
  12. for (int i = 0; i < length / 2; i++): This line starts a for loop that will iterate length / 2 times.
  13. if (input[i] != input[length - i - 1]): This line checks if the character at index i in the input string is not equal to the character at index length - i - 1 in the input string.
  14. isPalindrome = false;: If the characters are not equal, the isPalindrome variable is set to false.
  15. break;: If the characters are not equal, the loop is exited.
  16. Console.WriteLine("Is the input a palindrome? " + isPalindrome);: This line writes the message “Is the input a palindrome?” followed by the value of the isPalindrome variable to the console.
  17. 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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X