Consider the following method header and contract and an imp
Consider the following method header and contract, and an implementation body taken from the previous tracing table. For this question, example of strings that are palindrome include \"racecar\" and \"noon\", but not \"sam i am\" due to space characters/** * Reports whether a String is a palindrome. * * @ensures is Palindrome = [s reads the same way backward or forward] */private static Boolean is Palindrome(String s) {Boolean ans = false; int i = 0; int r = s.length() - 1; while(i
Solution
There is wrong in return false statement .
Here is code:
private static boolean isPalindrome(String s)
{
boolean ans = false;
int i = 0;
int r = s.length() -1;
while(i < s.length() / 2)
{
if(s.charAt(i) == s.charAt(r - i))
{
ans = true;
}
else
{
return false; // if any character doesn\'t match return false
}
i = i +1;
}
return ans;
}
