Thread: simple code
View Single Post
  #2 (permalink)  
Old 08-07-2007, 08:49 PM
leonard leonard is offline
Member
 
Join Date: Jul 2007
Posts: 43
leonard is on a distinguished road
You code will work only if the string lenght is odd. Wont work if it is even
Code:
boolean isPalindrome(String s) { int i=0, j = s.length( ) -1; while ( i !=j && s.charAt(i)==s.charAt(j)) { i++; j--; } return (i==j); } try these changes it will work boolean isPalindrome(String s) { int i=0, j = s.length( ) -1; while ( i !=j && s.charAt(i)==s.charAt(j)) { // change the condition here from i!=j to i<=j i++; j--; } return (i==j); //Change the condition here from i==j to i>j }
good luck
Reply With Quote