Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Palindrome Program

 
Jay Lesch
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I gotta make this program that can recognize a palindrome (word that is spelt the same forwards and back) and tell you if it is one or not.
I never grasped how to minipulate letters, I guess they're strings or chars. Cause I've had to do other programs before that required disecting a word letter for letter and then doing something with those letters, such as a morse code converter. I can do numbers fine, but the alphabet just screws me up

So yeah, any help with how to make the thing flip the word and then recognize if it's the same would be greatly appreciated. Just remember I'm like a total beginner so if you mention an operation like a loop, ya gotta explain that thouroughly. This may be one for e-mailing some code with commentary on it.
But this is the beginners forum so I trust I'll be helped apropraitly.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you should learn how to use the String class. You will probably find this tutorial a good place to start. You should also refer to the You will find the API documentation for the version of Java you are using. In particular, you should keep the the docs for the String class readily available so you can find a method to accomplish a given task.

I hope this helps you get started. Let us know what specific problems you encounter as you try to write this program.

Layne
 
Jay Lesch
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'm sure this'll help me. Right now, I'm rather tired so I'm not gonna be programming tonight. I just wanted to get my topic out there so that I could have something to work with tomorrow and the weekend if needed. I'll be sure to come back and post results or errors once I get going on it.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also checkout the API for StringBuffer. Could have some interesting methods there :-)

pascal
 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or i can help you in class! lol
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is your help, i already check it, It works!.

String word;
c.println ("Enter a word");
word = c.readLine ();
StringBuffer reverse = new StringBuffer ();
for (int count = word.length () - 1 ; count >= 0 ; count--)
{
reverse.append (word.charAt (count));
}
if (word.equals (reverse.toString ()))
c.println ("The word " + word + " is a palindrome");
else
c.println ("The word " + word + " is not a palindrome");
 
Ryan McGuire
Ranch Hand
Posts: 1061
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manuel Diaz:
Here is your help, i already check it, It works!.


Of course, I'm sure your school has a policy against passing someone else's work of as your own (a.k.a. plagerism).

Ryan
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are at least two ways to do this, by the way. One is to reverse the original string and see if it's the same, which is what Mr. Diaz has spelled out for you in vivid detail.

Of course, since that first way has already been spoiled, I'm sure you're now looking for a cool, different way to do it that you can figure out on your own, right? So try this algorithm on for size:

Compare the first letter to the last letter. If they're not the same, you're done--it's not a palindrome. If they are, look at the second letter and the second-to-last letter (if they exists) and check to see if they're the same. If they are, move to the... well, you get the idea.

Eventually, if it's a palindrome, you'll get to the middle of the String (which can be tricky to determine, since Strings can have an odd or even number of characters). If it's not, you'll eventually find a pair of letters that doesn't match.

For longer phrases in particular, this will probably be a more efficient test than reversing the entire string and checking every letter for equality.

- Jeff
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic