/** * A class containing some loop-based methods, for practice. */ public class Palindromes { /** * Takes a string as input and determines whether it's a palindrome * (spelled the same way forwards as backwards). * * @param word The word to be checked * @return true if word is a palindrome, false otherwise. */ public boolean isPalindrome(String word) { int front = 0; int back = word.length()-1; while (word.charAt(front)==word.charAt(back) && front < back) { front = front + 1; back = back - 1; } // Same issue as in findIndex_while -- there are two reasons the // loop might stop: Either because of a char mismatch, or because // we got all the way through and they all matched. if (word.charAt(front)==word.charAt(back)) { return true; } else { return false; } //return word.charAt(front)==word.charAt(back); } }