aspose file tools*
The moose likes Beginning Java and the fly likes Need a method for recognizing a pallindrome Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need a method for recognizing a pallindrome" Watch "Need a method for recognizing a pallindrome" New topic
Author

Need a method for recognizing a pallindrome

Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
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

Joined: Aug 24, 2001
Posts: 348
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 ]


not so smart guy still curious to learn new stuff every now and then
Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
can you elaborate? i cannot get the compiler to recognize reverse().
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
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

Joined: Jul 31, 2003
Posts: 263
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


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Do they still use that one as the standard homework assignment for recursion???


42
Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
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

Joined: Mar 02, 2003
Posts: 233
With weiss.nonstandard, it should look something like this pseudo code:

[ March 22, 2004: Message edited by: Gabriel White ]
Parth Sagdeo
Ranch Hand

Joined: Mar 18, 2004
Posts: 40
what if you tried something like:
Parth Sagdeo
Ranch Hand

Joined: Mar 18, 2004
Posts: 40
By the way, you could use the trim() method for cutting out excess spaces
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
This is what I would do...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Need a method for recognizing a pallindrome