• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need a method for recognizing a pallindrome

 
Douglas Braxton
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does any one have a coded method that will recognize a pallindrome within a String (same string whether backwards or forwards, i.e. BOB, MOM, DAD)?
[ March 19, 2004: Message edited by: Douglas Braxton ]
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one solution :
use reverse() method inside StringBuffer class
then compare the result with the original string.
Originally posted by Douglas Braxton:
Does any one have a coded method that will recognize a pallindrome within a String (same string whether backwards or forwards, i.e. BOB, MOM, DAD)?
[ March 19, 2004: Message edited by: Douglas Braxton ]
 
Douglas Braxton
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you elaborate? i cannot get the compiler to recognize reverse().
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
post your code, we discuss from there !
Originally posted by Douglas Braxton:
can you elaborate? i cannot get the compiler to recognize reverse().
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Douglas,
This is a great recursive problem. You method would check if the first and last characters of the string were the same, if so, create a new string consisting of the second through second to last characters of the string and recursively call your method with the new string.
Now for the base case. If the string is null, it's not a pallindrone. If the string is one character long, it's obviously a pallindrone. If it's two characters long, and the characters are the same, it's a pallindrone.
Post some code, and you'll get some more help. Most of the cowboys around these parts, don't like to post code when the problem appears to be homework related but we are more than happy to help once you've made an attempt.
Hope this helps,
Tom Blough
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do they still use that one as the standard homework assignment for recursion???
 
Douglas Braxton
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this one figured out. its an end to end comparison as noted. IGNORE the white space and compare apples to apples. or put differently
String s1 = " ";
is a different animal than
char s1 = ' ';
thanks everybody!!!
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With weiss.nonstandard, it should look something like this pseudo code:

[ March 22, 2004: Message edited by: Gabriel White ]
 
Parth Sagdeo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what if you tried something like:
 
Parth Sagdeo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, you could use the trim() method for cutting out excess spaces
 
David Peterson
author
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I would do...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic