---
title: "Finding a Palindrome"
url: "https://osherove.com/blog/2004/9/15/finding-a-palindrome.md"
canonical: "https://osherove.com/blog/2004/9/15/finding-a-palindrome.html"
published: "2004-09-15T23:01:00"
updated: "2004-09-15T23:01:00"
tags: [".Net Original"]
author: "Roy Osherove"
description: "[Update: I added a third variation of this which solves all the problems mentioned in the comments. Was really fun - used a single line of regular expressions and it worked like a charm!] It got me intrigued - so here's…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Finding a Palindrome

By Roy Osherove September 15, 2004 In [.Net Original](https://osherove.com/blog/category/net-original)

**Historical article** · Originally published September 15, 2004; technical details may be outdated.

\[Update: I added a third variation of this which solves all the problems mentioned in the comments. Was really fun - used a single line of regular expressions and it worked like a charm!\]

[It](http://blogs.msdn.com/jobsblog/archive/2004/08/25/220334.aspx) got me intrigued - so here's my answer to [finding a palindrome](http://channel9.msdn.com/ShowPost.aspx?PostID=19171) (a string you can read forward and backward the same):

\[Test\]

   public void TestPelindromSimple()

   {

         Assert.IsTrue(IsPalindrome("a"),"a");

         Assert.IsFalse(IsPalindrome("ab"),"ab");

         Assert.IsTrue(IsPalindrome("aba"),"aba");

         Assert.IsFalse(IsPalindrome("abc"),"abc");

         Assert.IsTrue(IsPalindrome("abba"),"abba");

         Assert.IsFalse(IsPalindrome("abbc"),"abbc");

         Assert.IsTrue(IsPalindrome("abcba"),"abcba");

         Assert.IsFalse(IsPalindrome("abcbc"),"abcbc");

         Assert.IsTrue(IsPalindrome("lelelel"),"lelelel");

         Assert.IsFalse(IsPalindrome("lele4el"),"lele4el");

         Assert.IsFalse(IsPalindrome(null),"null");

         Assert.IsTrue(IsPalindrome("ABba"),"ABba");

   }

   private bool IsPalindrome(string text)

   {

         if(text\==null)

         {

               return false;

         }

         if(text.Length\==1)

         {

               return true;

         }

         text\=text.ToLower();

         int firsthalfIndex = (text.Length/2);

         int secondHalfIndex\= (text.Length/2);

         if(text.Length%2!=0)

         {

               secondHalfIndex+=1;

         }

         char\[\] part1 = text.Substring(0,firsthalfIndex).ToCharArray();

         char\[\] part2 = text.Substring(secondHalfIndex).ToCharArray();

         Array.Reverse(part2);

         return (new string( part1)== new string(part2));

   }

I liked what I had but then it occurred to me: all these test can pass with this simple routine:

private bool IsPalindrome(string text)

 {

   if(text\==null)

   {

      return false;

   }

   text\=text.ToLower();

   char\[\] backwards = text.ToCharArray();

   Array.Reverse(backwards);

   return (new string( backwards)== text);

 }

Ain't life funny?

\[Update\]

This variation adds a single line of regex to comply with the dictionary form of Palindrome.

I also added a couple of unit tests to make sure it works:

Assert.IsTrue(IsPalindrome("ab cb a"),"ab cb a");

Assert.IsTrue(IsPalindrome("A man, a plan, a canal, Panama!"),"A man, a plan, a canal, Panama!");

private bool IsPalindrome(string text)

{

   if(text\==null){

      return false;

   }

  text\=text.ToLower();

  //remove whitespace and other non numeric chars

  **text = Regex.Replace(text,@"\[\\s\\W\]",string.Empty);**

  char\[\] backwards = text.ToCharArray();

  Array.Reverse(backwards);

  return (new string( backwards)== text);

}

 

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- Sep 22, 2004

    ### [ANN: Optimized automatic control binder is out - and its fast!](https://osherove.com/blog/2004/9/22/ann-optimized-automatic-control-binder-is-out-and-its-fast.html)

- Sep 21, 2004

    ### [One treeview I'm not going to download](https://osherove.com/blog/2004/9/21/one-treeview-im-not-going-to-download.html)

- Sep 9, 2004

    ### [How Test Driven Development proved itself in a big way!](https://osherove.com/blog/2004/9/9/how-test-driven-development-proved-itself-in-a-big-way.html)

- Sep 8, 2004

    ### [See you at the .Net on the beach convention](https://osherove.com/blog/2004/9/8/see-you-at-the-net-on-the-beach-convention.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
