-
simple code
The following function determine if its parameter is a string which reads exactly the same forward or backward. There is an error in the code.
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);
}
help please
-
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