Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check if String is Palindrome using C#?
A palindrome is a string that reads the same forwards and backwards, such as "Level", "madam", or "racecar". In C#, there are several ways to check if a string is a palindrome.
Using Array.Reverse() Method
The simplest approach is to reverse the string and compare it with the original. First, convert the string to a character array −
char[] ch = str.ToCharArray();
Then reverse the array using Array.Reverse() −
Array.Reverse(ch);
Finally, compare the reversed string with the original using case-insensitive comparison −
bool res = str.Equals(rev, StringComparison.OrdinalIgnoreCase);
Example
using System;
class Program {
static void Main(string[] args) {
string str, rev;
str = "Level";
char[] ch = str.ToCharArray();
Array.Reverse(ch);
rev = new string(ch);
bool res = str.Equals(rev, StringComparison.OrdinalIgnoreCase);
if (res == true) {
Console.WriteLine("String " + str + " is a Palindrome!");
} else {
Console.WriteLine("String " + str + " is not a Palindrome!");
}
}
}
The output of the above code is −
String Level is a Palindrome!
Using Two-Pointer Technique
A more efficient approach uses two pointers to compare characters from both ends moving towards the center −
using System;
class Program {
static bool IsPalindrome(string str) {
int left = 0;
int right = str.Length - 1;
while (left < right) {
if (char.ToLower(str[left]) != char.ToLower(str[right])) {
return false;
}
left++;
right--;
}
return true;
}
static void Main(string[] args) {
string[] testStrings = {"Level", "hello", "racecar", "A man a plan a canal Panama"};
foreach (string str in testStrings) {
string cleanStr = str.Replace(" ", "");
if (IsPalindrome(cleanStr)) {
Console.WriteLine("'" + str + "' is a Palindrome!");
} else {
Console.WriteLine("'" + str + "' is not a Palindrome!");
}
}
}
}
The output of the above code is −
'Level' is a Palindrome! 'hello' is not a Palindrome! 'racecar' is a Palindrome! 'A man a plan a canal Panama' is a Palindrome!
Using LINQ Reverse()
You can also use LINQ to reverse the string and compare −
using System;
using System.Linq;
class Program {
static void Main(string[] args) {
string str = "madam";
string reversed = new string(str.Reverse().ToArray());
bool isPalindrome = string.Equals(str, reversed, StringComparison.OrdinalIgnoreCase);
if (isPalindrome) {
Console.WriteLine("'" + str + "' is a Palindrome!");
} else {
Console.WriteLine("'" + str + "' is not a Palindrome!");
}
}
}
The output of the above code is −
'madam' is a Palindrome!
Performance Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Array.Reverse() | O(n) | O(n) | Simple implementation |
| Two-Pointer | O(n/2) | O(1) | Memory efficiency |
| LINQ Reverse() | O(n) | O(n) | Functional programming style |
Conclusion
Checking for palindromes in C# can be accomplished using various methods. The two-pointer technique is most efficient for memory usage, while Array.Reverse() offers simplicity. Choose the method that best fits your performance requirements and coding style preferences.
