• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to check whether a word is a palindrome

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ");
}}
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any one tried this ?

 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic