0
0
CsharpProgramBeginner · 2 min read

C# Program to Check Palindrome String

You can check a palindrome string in C# by comparing the original string with its reversed version using string reversed = new string(input.Reverse().ToArray()); and then checking input == reversed.
📋

Examples

Inputmadam
Outputmadam is a palindrome.
Inputhello
Outputhello is not a palindrome.
InputA
OutputA is a palindrome.
🧠

How to Think About It

To check if a string is a palindrome, think of reading it from left to right and right to left. If both readings are exactly the same, the string is a palindrome. So, reverse the string and compare it with the original. If they match, it is a palindrome; otherwise, it is not.
📐

Algorithm

1
Get the input string.
2
Reverse the input string.
3
Compare the original string with the reversed string.
4
If both are equal, return that the string is a palindrome.
5
Otherwise, return that it is not a palindrome.
💻

Code

csharp
using System;
using System.Linq;

class Program {
    static void Main() {
        Console.Write("Enter a string: ");
        string input = Console.ReadLine();
        string reversed = new string(input.Reverse().ToArray());
        if (input == reversed) {
            Console.WriteLine($"{input} is a palindrome.");
        } else {
            Console.WriteLine($"{input} is not a palindrome.");
        }
    }
}
Output
Enter a string: madam madam is a palindrome.
🔍

Dry Run

Let's trace the input 'madam' through the code.

1

Input string

input = 'madam'

2

Reverse string

reversed = 'madam'

3

Compare strings

input == reversed -> 'madam' == 'madam' is true

4

Output result

Print 'madam is a palindrome.'

StepinputreversedComparison Result
1madam
2madammadam
3madammadamtrue
💡

Why This Works

Step 1: Reverse the string

We use input.Reverse() to get characters in reverse order and create a new string from them.

Step 2: Compare original and reversed

If the original string and reversed string are exactly the same, it means the string reads the same forwards and backwards.

Step 3: Print result

Based on the comparison, we print whether the string is a palindrome or not.

🔄

Alternative Approaches

Two-pointer approach
csharp
using System;

class Program {
    static bool IsPalindrome(string s) {
        int left = 0, right = s.Length - 1;
        while (left < right) {
            if (s[left] != s[right]) return false;
            left++;
            right--;
        }
        return true;
    }

    static void Main() {
        Console.Write("Enter a string: ");
        string input = Console.ReadLine();
        if (IsPalindrome(input)) {
            Console.WriteLine($"{input} is a palindrome.");
        } else {
            Console.WriteLine($"{input} is not a palindrome.");
        }
    }
}
This method checks characters from both ends moving inward without creating a new string, saving memory.
Using for loop to build reversed string
csharp
using System;

class Program {
    static void Main() {
        Console.Write("Enter a string: ");
        string input = Console.ReadLine();
        string reversed = "";
        for (int i = input.Length - 1; i >= 0; i--) {
            reversed += input[i];
        }
        if (input == reversed) {
            Console.WriteLine($"{input} is a palindrome.");
        } else {
            Console.WriteLine($"{input} is not a palindrome.");
        }
    }
}
This builds the reversed string manually but is less efficient due to string concatenation in a loop.

Complexity: O(n) time, O(n) space

Time Complexity

Reversing the string takes O(n) time where n is the string length, and comparing two strings also takes O(n). Overall complexity is O(n).

Space Complexity

Creating a reversed string requires O(n) extra space. The two-pointer approach can reduce space to O(1).

Which Approach is Fastest?

The two-pointer approach is fastest in space as it avoids extra string creation, but using Reverse() is simpler and clear for beginners.

ApproachTimeSpaceBest For
Using string.Reverse()O(n)O(n)Simplicity and readability
Two-pointer approachO(n)O(1)Memory efficiency
Manual for loop reversalO(n)O(n)Understanding string manipulation basics
💡
Use string.Reverse() with ToArray() to quickly reverse a string in C#.
⚠️
Forgetting that strings are case-sensitive and not normalizing case before comparison can cause wrong results.