This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to checker user enter is palindrome Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to checker user enter is palindrome" Watch "How to checker user enter is palindrome" New topic
Author

How to checker user enter is palindrome

Phylisku
Greenhorn

Joined: Dec 03, 2008
Posts: 2
Appreciated if replies
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
Reading this is probably a good place to start - especially the first link.


Joanne
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Psychoh@cker please change your name in accordance with our Naming policies, thanks!


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

One hint: a word or sentence is a palindrome if it is the same after you reverse it. I'm sure you can find a method or set of methods to reverse a string.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Another:

A word is a palindrome if its length is <= 1.
A word is a palindrome if its first and last characters are the same and the letters in between are a palindrome.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38020
    
  22
Originally posted by Martijn Verburg:
Psychoh@cker please change your name in accordance with our Naming policies, thanks!
Fyllikus? That's no better
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38020
    
  22
You obviously want your account closed; the naming policy is not optional. Change your displayed name.

CR
Phylisku
Greenhorn

Joined: Dec 03, 2008
Posts: 2
Can please show java code for check palimdrome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38020
    
  22
Originally posted by Phylisku:
Can please show java code for check palimdrome


No
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Check out the StringBuffer class (specially the reverse method)


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38020
    
  22
It had never occurred to me that you can do it recursively until I saw Garrett's method. Nice one!
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
Originally posted by Garrett Rowe:
Another:

A word is a palindrome if its length is <= 1.
A word is a palindrome if its first and last characters are the same and the letters in between are a palindrome.


I like this...I've never heard of it before, only took me 12 lines of code including a try/catch and ending braces.

@ the OP. Take those 2 statements right there. Turn them into java code. If you come back with something written we'd be more than willing to help with specific questions. Most people don't want to help people who aren't willing to help themself.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Originally posted by Campbell Ritchie:
It had never occurred to me that you can do it recursively until I saw Garrett's method. Nice one!

You can even do it with a simple loop.

I did it in 11 lines, excluding try-catch (why would you need that?) with starting braces on new lines. Also no unncessary long lines (i.e. over 80 characters) or anything. I doubt it could be done any faster either.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1753
    
    7

Originally posted by Campbell Ritchie:
It had never occurred to me that you can do it recursively until I saw Garrett's method. Nice one!


Seriously? When I was being taught about data structures and algorithms one of the very first exercises used to familiarize us with the concepts of recursion was determining if a character string was palindromic. Somehow I've always thought of it as the universal textbook example of a recursive function. Weird


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2995
    
    9
I think of Tower of Hanoi as the textbook recursion problem. But I'm sure there are others as well.

[Rob]: I did it in 11 lines, excluding try-catch (why would you need that?) with starting braces on new lines. Also no unncessary long lines (i.e. over 80 characters) or anything. I doubt it could be done any faster either.

I look at it as four lines of actual implementation, plus one line for a method declaration, plus as many lines of braces as your coding style requires. For me it's a six-line method, but others might well format it to ten lines. I expect we're talking about basically the same code though - perhaps the eleventh line is used capturing the length() in a variable?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38020
    
  22
No, the 11th line is used for counting the other lines to see whether it's shorter than everybody else's solution
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
Originally posted by Rob Prime:

I did it in 11 lines, excluding try-catch (why would you need that?)

I'm sure yours is shorter, faster, stronger, amazinger, and wrote itself so I'm sorry.

I passed a substring into the same method after checking the first and last...if we substring from (1,1) on a substring of 2 length it's out of bounds. At this point you know it's a palindrome so I just caught it and returned true. Different strokes for different folks.

<psuedo>

</psuedo>

My point was that this didn't take long to do...from start to finish...mine was 12 lines including a try/catch that if I took the time to perfect the code the number of lines would decrease. He wasn't even attempting.
[ December 05, 2008: Message edited by: Paul Yule ]
Doe oeD
Greenhorn

Joined: Dec 04, 2008
Posts: 1
If is using ADT stack and queue.How would you do that? Show code
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

Originally posted by Doe oeD:
If is using ADT stack and queue.How would you do that? Show code


"Doe oeD"
Please check your private messages for an important administrative matter


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

And we don't just "show code".
 
 
subject: How to checker user enter is palindrome