The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Finding a Palindrome

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Roy Osherove

Posts: 1807
Nickname: royo
Registered: Sep, 2003

Roy Osherove is a .Net consultant based in Israel
Finding a Palindrome Posted: Sep 16, 2004 4:49 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Roy Osherove.
Original Post: Finding a Palindrome
Feed Title: ISerializable
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/rosherove/Rss.aspx
Feed Description: Roy Osherove's persistent thoughts
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Roy Osherove
Latest Posts From ISerializable

Advertisement
 
It got me intrigued - so here's my answer to finding a palindrome (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?

Read: Finding a Palindrome

Topic: Back to your regularly scheduled technology blog: ASP.NET ClientSide JavaScript Fix for... Previous Topic   Next Topic Topic: NerdDinner.com

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use