File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to check whether a word is a palindrome Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to check whether a word is a palindrome" Watch "how to check whether a word is a palindrome" New topic
Author

how to check whether a word is a palindrome

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
We have had a fascinating discussion, but I keep forgetting that this thread doesn't conform to this FAQ. So I shall pull rank and edit the title!
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
Hey man why your making that program that much complicated ?

Just try this simply with the help of array we can do that same thing ?

The code is as follows :

class grp
{

public static void main(String a[])
{
int i;
String str[]={"NITIN"};
int len=str.length;

for(i=0;i<len/2;i++)
{

if(str[i]==str[--len])
continue;

else
System.out.println("Not paliandrome");
break;
}
System.out.println("Paliandrome ");
}}


Anto Telvin Mathew<br />Many of the life failures are people who did not realize how close they were to success when they give up. EDISON
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
Originally posted by Anto telvin Mathew:
Hey man why your making that program that much complicated ?


Just to show that there are many ways to achieve the same result.
You might want to test your code with a non-palindrome. I suspect you won't get quite the result you expected.

Edit - in fact you might want to try stepping through it. Even with a valid palindrome, it's not doing what you think it's doing.
[ August 14, 2008: Message edited by: Joanne Neal ]

Joanne
Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
Any one tried this ?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Originally posted by Anto telvin Mathew:
Hey man why your making that program that much complicated ?
Some offerings have been more complicated than yours, and some simpler.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Originally posted by Graeme Byers:
Any one tried this ?


I know it exists, and it could be used for this problem quite well indeed.

However, from a performance point, I'd say that just iterating through half the string will be more efficient than all other solutions. Please note that the recursive method basically does the same, so it's just as efficient*.
Reversing the string requires a) iterating over the string once (although StringBuilder.reverse swaps elements, so again half the string in operations), then b) iterating over both strings again (for the equals test). And to make it worse, you need an extra object.

Sure, nearly all solutions shown are still O(n), but the difference between 10 minutes and 5 minutes (for extremely long strings ) is still worth the trouble.


* Well, perhaps for an increasing stack - every call will put the arguments and local variables on the stack. A loop won't.
[ August 14, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to check whether a word is a palindrome