
Palindromes can be read in both directions. How can you determine if a string is a palindrome in the C# language? A palindrome has the same letters on both ends of the string. This means "civic" is a palindrome, but "perls" is not.
This C# algorithm example determines if a string is a palindrome.
This program introduces the IsPalindrome boolean method. In the method body, we determine the minimum and maximum indexes inside the string. Then, we scan from the minimum forward, and from the maximum backward, at the same time. If the characters are different, we return false; if we are done scanning, we return true.
Program that checks for palindromic strings [C#]
using System;
class Program
{
/// <summary>
/// Determines whether the string is a palindrome.
/// </summary>
public static bool IsPalindrome(string value)
{
int min = 0;
int max = value.Length - 1;
while (true)
{
if (min > max)
{
return true;
}
char a = value[min];
char b = value[max];
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
min++;
max--;
}
}
static void Main()
{
string[] array =
{
"civic",
"deified",
"deleveled",
"devoved",
"dewed",
"Hannah",
"kayak",
"level",
"madam",
"racecar",
"radar",
"redder",
"refer",
"repaper",
"reviver",
"rotator",
"rotor",
"sagas",
"solos",
"sexes",
"stats",
"tenet",
"Dot",
"Net",
"Perls",
"Is",
"Not",
"A",
"Palindrome",
""
};
foreach (string value in array)
{
Console.WriteLine("{0} = {1}", value, IsPalindrome(value));
}
}
}
Output
civic = True
deified = True
deleveled = True
devoved = True
dewed = True
Hannah = True
kayak = True
level = True
madam = True
racecar = True
radar = True
redder = True
refer = True
repaper = True
reviver = True
rotator = True
rotor = True
sagas = True
solos = True
sexes = True
stats = True
tenet = True
Dot = False
Net = False
Perls = False
Is = False
Not = False
A = True
Palindrome = False
= True
Case-insensitive. The IsPalindrome method is case-insensitive. Instead of converting the input string with ToLower, which would require an allocation, we simply check the lowercased versions of all the characters. This makes it a bit faster. If your input strings are always lowercase, it would be better to remove the char.ToLower transformations.
char.ToLower MethodsExceptions. The method will throw an exception if the parameter string is null. If the parameter string is empty, it will consider that a palindrome. This is correct by the definition of palindrome specified by the method.
Exception Handling
This program is the same as the simpler palindrome method except for some small changes. The important change is inside IsPalindrome. There are two additional while loops. If either char is determined to be a non-letter non-digit character, we begin skipping forward or backward to one that should be checked. This means that punctuation and spaces do not affect the palindromicity of a phrase.
Program that checks for palindromic sentences [C#]
using System;
class Program
{
/// <summary>
/// Determines whether the string is a palindrome.
/// </summary>
public static bool IsPalindrome(string value)
{
int min = 0;
int max = value.Length - 1;
while (true)
{
if (min > max)
{
return true;
}
char a = value[min];
char b = value[max];
// Scan forward for a while invalid.
while (!char.IsLetterOrDigit(a))
{
min++;
if (min > max)
{
return true;
}
a = value[min];
}
// Scan backward for b while invalid.
while (!char.IsLetterOrDigit(b))
{
max--;
if (min > max)
{
return true;
}
b = value[max];
}
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
min++;
max--;
}
}
static void Main()
{
string[] array =
{
"A man, a plan, a canal: Panama.",
"A Toyota. Race fast, safe car. A Toyota.",
"Cigar? Toss it in a can. It is so tragic.",
"Dammit, I'm mad!",
"Delia saw I was ailed.",
"Desserts, I stressed!",
"Draw, O coward!",
"Lepers repel.",
"Live not on evil.",
"Lonely Tylenol.",
"Murder for a jar of red rum.",
"Never odd or even.",
"No lemon, no melon.",
"Senile felines.",
"So many dynamos!",
"Step on no pets.",
"Was it a car or a cat I saw?",
"Dot Net Perls is not a palindrome.",
"Why are you reading this?",
"This article is not very useful.",
"...",
"...Test"
};
foreach (string value in array)
{
Console.WriteLine("{0} = {1}", value, IsPalindrome(value));
}
}
}
Output
A man, a plan, a canal: Panama. = True
A Toyota. Race fast, safe car. A Toyota. = True
Cigar? Toss it in a can. It is so tragic. = True
Dammit, I'm mad! = True
Delia saw I was ailed. = True
Desserts, I stressed! = True
Draw, O coward! = True
Lepers repel. = True
Live not on evil. = True
Lonely Tylenol. = True
Murder for a jar of red rum. = True
Never odd or even. = True
No lemon, no melon. = True
Senile felines. = True
So many dynamos! = True
Step on no pets. = True
Was it a car or a cat I saw? = True
Dot Net Perls is not a palindrome. = False
Why are you reading this? = False
This article is not very useful. = False
... = True
...Test = False
Performance. What is the advantage of using two subloops to skip forward and back while checking for palindromes? This approach means you do not need to allocate any new strings. You can check the original string data without converting it. A slower approach would be to strip all spaces and punctuation from the source string. Then, you could use the simpler palindrome method.
Note: We examined an alternative version of the IsPalindrome method that skips past punctuation and spaces. This makes it possible to check strings for palindromicity without converting them first. By using subloops in the IsPalindrome method, we can skip past characters we want to ignore, improving the effectiveness of our algorithm.

In this article, we introduced a very simple method that tests for palindromes in the C# programming language. Many palindromes are composed of multiple words and have spaces and punctuation. These can be tested with a separate method that accounts for these characters.
Algorithms