JAVA Code: Complete the method, isPalindrome(), that takes in a String and retur
ID: 3917561 • Letter: J
Question
JAVA Code:
Complete the method, isPalindrome(), that takes in a String and returns a boolean. The boolean should be true if the String is a palindrome, i.e., the String is the same if read forwards or in reverse. For example, "racecar" is a palindrome. The method should return false if the String is not a palindrome. Note: any single-letter word is considered a palindrome.
Starter Code:
public class StringMethod {
public static boolean isPalindrome(String word) {
//TODO: Complete this method
}
}
Explanation / Answer
If you have any doubts, please give me comment...
public class StringMethod {
public static boolean isPalindrome(String word) {
// TODO: Complete this method
int len = word.length();
for (int i = 0; i < len / 2; i++) {
if (word.charAt(i) != word.charAt(len - i - 1))
return false;
}
return true;
}
//remove main method if you don't need...
public static void main(String[] args) {
System.out.println(isPalindrome("racecar"));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.