Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to checker user enter is palindrome

 
Phylisku
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Appreciated if replies
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reading this is probably a good place to start - especially the first link.
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Psychoh@cker please change your name in accordance with our Naming policies, thanks!
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You obviously want your account closed; the naming policy is not optional. Change your displayed name.

CR
 
Phylisku
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can please show java code for check palimdrome
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Phylisku:
Can please show java code for check palimdrome


No
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the StringBuffer class (specially the reverse method)
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It had never occurred to me that you can do it recursively until I saw Garrett's method. Nice one!
 
Paul Yule
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If is using ADT stack and queue.How would you do that? Show code
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And we don't just "show code".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic