12,396,461 members (64,714 online)
alternative version

47.8K views
12 bookmarked
Posted

# Palindromes in C#

, 28 Aug 2008 CPOL
 Rate this:
Algorithim for calculating if a string is a palindrome.

## Introduction

This code calculates if a string is a palindrome, and was inspired by the Channel9 video by Gary Daniels and Evan Goldring - Mock whiteboard problem.

## Background

There are two main approaches to calculating palindromes: byte symmetry, or on the basis of words. Byte symmetry checks to see if a string is a mirror image of itself "helloolleh", whilst the other approach handles white spaces "Was it Eliot's toilet I saw?"

## Using the code

I've not inluded the code as an attachment, rather as inline text that can be used directly.

```static bool isPalindrome(string subject)
{
const int STEP_FORWARD = 1;
const int STEP_BACKWARD = -STEP_FORWARD;
const int BEFORE_START = -1;
const int CHAR_AT_A_TIME = 1;
const int COMPARE_EQUALS = 0;

// assume its not a palindrome
bool result = false;

// how to compare
CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;
CompareOptions co =
CompareOptions.IgnoreCase |
CompareOptions.IgnoreKanaType |
CompareOptions.IgnoreNonSpace |
CompareOptions.IgnoreSymbols |
CompareOptions.IgnoreWidth; |

// null strings are not palindromes
if (subject != null)
{
int AFTER_END = subject.Length;

// single letter words are palindromes
result = (AFTER_END == 1 && IsPalindromeChar(subject[0]));

// start the comparison points at valid characters
int startOffset = GetNextValidCharacter(subject, BEFORE_START,
STEP_FORWARD, AFTER_END);
int endOffset = GetNextValidCharacter(subject, AFTER_END,
STEP_BACKWARD, BEFORE_START);

while (startOffset < endOffset)
{
result = ci.Compare(subject, startOffset, CHAR_AT_A_TIME,
subject, endOffset, CHAR_AT_A_TIME, co) == COMPARE_EQUALS;

if (!result)
break;

// move the comparison points towards each other
startOffset = GetNextValidCharacter(subject, startOffset,
STEP_FORWARD, endOffset);
endOffset = GetNextValidCharacter(subject, endOffset,
STEP_BACKWARD, startOffset);
}
}

return result;
}

static int GetNextValidCharacter(string subject, int offset,
int step, int bound)
{
if (offset != bound)
offset += step;

while (offset != bound && !IsPalindromeChar(subject[offset]))
offset += step;

return offset;
}

static bool IsPalindromeChar(char c)
{
return char.IsLetter(c) || char.IsDigit(c);
}```

## Points of interest

Reading the Wikipedia entry on palindromes shows that there are more than just alphabetical palindromes, so it is worth checking with your users before assuming that this code is what they mean by a palindrome.

On my blog, I've also included the byte symmetry code approach.

## History

• Aug/2008 - First version.

## Share

 Architect United Kingdom
still alive, just.
Campaign to bring back Windows 3.11 Look-N-Feel!

## You may also be interested in...

 First Prev Next
 Simplified for C# ZamirF3-Feb-11 9:32 ZamirF 3-Feb-11 9:32
 on the basis of words? PIEBALDconsult29-Aug-08 11:42 PIEBALDconsult 29-Aug-08 11:42
 Re: on the basis of words? Philip Fitzsimons30-Aug-08 0:29 Philip Fitzsimons 30-Aug-08 0:29
 Another approach Ravi Bhavnani28-Aug-08 9:26 Ravi Bhavnani 28-Aug-08 9:26
 Re: Another approach PIEBALDconsult28-Aug-08 9:46 PIEBALDconsult 28-Aug-08 9:46
 Re: Another approach Ravi Bhavnani28-Aug-08 9:48 Ravi Bhavnani 28-Aug-08 9:48
 Re: Another approach PIEBALDconsult28-Aug-08 10:26 PIEBALDconsult 28-Aug-08 10:26
 Re: Another approach Ravi Bhavnani28-Aug-08 10:30 Ravi Bhavnani 28-Aug-08 10:30
 Re: Another approach PIEBALDconsult28-Aug-08 10:46 PIEBALDconsult 28-Aug-08 10:46
 Re: Another approach Ravi Bhavnani28-Aug-08 10:49 Ravi Bhavnani 28-Aug-08 10:49
 Re: Another approach Philip Fitzsimons28-Aug-08 23:48 Philip Fitzsimons 28-Aug-08 23:48
 Re: Another approach Ravi Bhavnani29-Aug-08 3:45 Ravi Bhavnani 29-Aug-08 3:45
 Re: Another approach Jared Mcguire29-Aug-08 9:21 Jared Mcguire 29-Aug-08 9:21
 Re: Another approach PIEBALDconsult29-Aug-08 11:19 PIEBALDconsult 29-Aug-08 11:19
 Oh goody PIEBALDconsult28-Aug-08 6:35 PIEBALDconsult 28-Aug-08 6:35
 Re: Oh goody Philip Fitzsimons28-Aug-08 23:43 Philip Fitzsimons 28-Aug-08 23:43
 Re: Oh goody PIEBALDconsult29-Aug-08 4:22 PIEBALDconsult 29-Aug-08 4:22
 Re: Oh goody Philip Fitzsimons29-Aug-08 5:12 Philip Fitzsimons 29-Aug-08 5:12
 Re: Oh goody [modified] PIEBALDconsult29-Aug-08 10:28 PIEBALDconsult 29-Aug-08 10:28
 Re: Oh goody Philip Fitzsimons30-Aug-08 0:27 Philip Fitzsimons 30-Aug-08 0:27
 Re: Oh goody PIEBALDconsult30-Aug-08 7:58 PIEBALDconsult 30-Aug-08 7:58
 Last Visit: 31-Dec-99 18:00     Last Update: 24-Jul-16 17:07 Refresh 1