Click here to Skip to main content
15,884,986 members
Articles / Programming Languages / C#
Article

Palindromes in C#

Rate me:
Please Sign up or sign in to vote.
4.33/5 (4 votes)
28 Aug 2008CPOL 65.4K   14   21
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.

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions

 
GeneralSimplified for C# Pin
ZamirF3-Feb-11 9:32
ZamirF3-Feb-11 9:32 
Questionon the basis of words? Pin
PIEBALDconsult29-Aug-08 11:42
mvePIEBALDconsult29-Aug-08 11:42 
AnswerRe: on the basis of words? Pin
Philip Fitzsimons30-Aug-08 0:29
Philip Fitzsimons30-Aug-08 0:29 
GeneralAnother approach Pin
Ravi Bhavnani28-Aug-08 9:26
professionalRavi Bhavnani28-Aug-08 9:26 
GeneralRe: Another approach Pin
PIEBALDconsult28-Aug-08 9:46
mvePIEBALDconsult28-Aug-08 9:46 
GeneralRe: Another approach Pin
Ravi Bhavnani28-Aug-08 9:48
professionalRavi Bhavnani28-Aug-08 9:48 
GeneralRe: Another approach Pin
PIEBALDconsult28-Aug-08 10:26
mvePIEBALDconsult28-Aug-08 10:26 
GeneralRe: Another approach Pin
Ravi Bhavnani28-Aug-08 10:30
professionalRavi Bhavnani28-Aug-08 10:30 
GeneralRe: Another approach Pin
PIEBALDconsult28-Aug-08 10:46
mvePIEBALDconsult28-Aug-08 10:46 
GeneralRe: Another approach Pin
Ravi Bhavnani28-Aug-08 10:49
professionalRavi Bhavnani28-Aug-08 10:49 
GeneralRe: Another approach Pin
Philip Fitzsimons28-Aug-08 23:48
Philip Fitzsimons28-Aug-08 23:48 
GeneralRe: Another approach Pin
Ravi Bhavnani29-Aug-08 3:45
professionalRavi Bhavnani29-Aug-08 3:45 
GeneralRe: Another approach Pin
Jaguire29-Aug-08 9:21
Jaguire29-Aug-08 9:21 
GeneralRe: Another approach Pin
PIEBALDconsult29-Aug-08 11:19
mvePIEBALDconsult29-Aug-08 11:19 
GeneralOh goody Pin
PIEBALDconsult28-Aug-08 6:35
mvePIEBALDconsult28-Aug-08 6:35 
GeneralRe: Oh goody Pin
Philip Fitzsimons28-Aug-08 23:43
Philip Fitzsimons28-Aug-08 23:43 
GeneralRe: Oh goody Pin
PIEBALDconsult29-Aug-08 4:22
mvePIEBALDconsult29-Aug-08 4:22 
Note the cleverly hidden ToUpper.
GeneralRe: Oh goody Pin
Philip Fitzsimons29-Aug-08 5:12
Philip Fitzsimons29-Aug-08 5:12 
GeneralRe: Oh goody [modified] Pin
PIEBALDconsult29-Aug-08 10:28
mvePIEBALDconsult29-Aug-08 10:28 
GeneralRe: Oh goody Pin
Philip Fitzsimons30-Aug-08 0:27
Philip Fitzsimons30-Aug-08 0:27 
GeneralRe: Oh goody Pin
PIEBALDconsult30-Aug-08 7:58
mvePIEBALDconsult30-Aug-08 7:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.