• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to check whether a word is a palindrome

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ");
}}
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any one tried this ?

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic