Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Palindrome

 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code but I can't seem to get this phrase to output the same. The phrase is "A man a plan a canal panama"
 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarrell, I edited your code to use the "Code" tags. Looks a lot more readable, right? You can use them yourself in future, just click on the Code button and post your code between the tags you get.

Anyway, what exactly is your question? I would expect that code to output "This is not a Palindrome". Did it not do that? Or what were you expecting?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the result of reverseIt is: amanap lanac a nalp a nam A. Right away we are in trouble because the String.equals will fail as soon as it compares 'A' to 'a'. So your comparison needs to be case-insensitive. After you solve that, the String.equals will fail when it compares 'm' to ' '.

Note that palindromes ignore case, spaces, and punctuation. This celebrated palindrome is often rendered as: "A man, a plan, a canal: Panama!". So before you begin your String comparison, you'll want to strip out anything that is not a letter.
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote: So before you begin your String comparison, you'll want to strip out anything that is not a letter.


Digits?
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsha Smith wrote: . . . Digits?
If you are using words, yes. You can have numbers which you regard as palindromic, eg 12321, but that is a different problem.
 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found out how to fix it. I just needed to close up any whitespace so when I typed a word with a space it wouldn't see that blank. My assignment was to show the reversed code with no spaces so I guess that was it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic