BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Algorithms and Data Structures
.NET 1.1+

Palindrome Tester

A palindrome is a word or phrase that reads the same backwards as it does forwards, for example "Madam, I'm Adam". This article describes a simple method for testing the validity of palindrome using basic string manipulation functionality.

What is a Palindrome?

A palindrome is a word, phrase or series of numbers that reads the same backwards as forwards. Palindromes can be very short. For example, the word "Pop" is a palindrome. They may also be very long phrases. In these cases, the punctuation and spaces are not considered when determining if a phrase is a palindrome. A well-known example of such a phrase is "A man, a plan, a canal - Panama!"

Checking if a String is a Palindrome

In this article we will create a simple method that checks if a given string is a palindrome. The method will follow a three-stage process. Firstly the string will be prepared, secondly it will be reversed and finally a comparison of the result with the original string will be made.

To begin, create a new console application project and add a new method to the Program class as follows.

static bool IsPalindrome(string phrase)
{
}

On completion of the method, it will return true if the string parameter contains a palindrome and false otherwise.

Preparing the String

The first stage of the process is to prepare the string. All punctuation and spaces will be removed and the string will be converted to lower case so that when reversed, no further work will be required before performing a comparison of the original and the reversed string.

A string has an enumerator that allows you to iterate through each of the characters it contains, using a foreach loop. We can use this to process one character at a time, in the correct order, and add the character to a separate variable only if it is a letter or numeric digit. To perform the check, we can use the static IsLetterOrDigit method of the char class.

As it is inefficient to perform multiple modifications to a string, the characters will be added to a StringBuilder object. On completion of the loop, the final StringBuilder contents can be copied into a string, in this case named "forwards". To implement this step of the process, add the following code to the method:

StringBuilder cleanPhraseBuilder = new StringBuilder();

foreach (char c in phrase)
{
    if (char.IsLetterOrDigit(c)) cleanPhraseBuilder.Append(c);
}

string forwards = cleanPhraseBuilder.ToString().ToLower();

Reversing the String

Strings can be easily reversed using standard array functionality. This is described in the article, "Reversing a String". Add the code below to the method to perform this step, holding the result in the "backwards" variable:

char[] characters = forwards.ToCharArray();
Array.Reverse(characters);
string backwards = new string(characters);

Comparing the Strings

The final step is to compare the "forwards" and "backwards" string using the equality operator. This operator returns either true or false so can be returned directly by adding the final line of code to the method.

return forwards == backwards;

Testing the Code

We can test the code by executing the method for valid and invalid palindromes. Try the following examples by adding the code to the Main method of the program:

bool palindrome;

palindrome = IsPalindrome("Madam, I'm Adam!");  // Returns true
palindrome = IsPalindrome("Madam, I am Adam!"); // Returns false
23 September 2008