• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Simple Palindrome Program

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also checkout the API for StringBuffer. Could have some interesting methods there :-)

pascal
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or i can help you in class! lol
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Did you just should on me? You should read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic