jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Simple Palindrome Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple Palindrome Program" Watch "Simple Palindrome Program" New topic

Simple Palindrome Program

Jay Lesch

Joined: Apr 21, 2005
Posts: 2
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.

My teacher pronounces java as "jawa", help me
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.


Java API Documentation
The Java Tutorial
Jay Lesch

Joined: Apr 21, 2005
Posts: 2
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

Joined: Jun 19, 2001
Posts: 547
also checkout the API for StringBuffer. Could have some interesting methods there :-)

Clay Adkerson

Joined: Mar 16, 2005
Posts: 27
Or i can help you in class! lol
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
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");
c.println ("The word " + word + " is not a palindrome");

Note: I love programming.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1044
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).

Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
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
I agree. Here's the link: http://aspose.com/file-tools
subject: Simple Palindrome Program
jQuery in Action, 3rd edition