public boolean isPalindrome() { int length = text.length(); // Separate case for shortest strings. if (length <= 1) return true; // Get first and last characters, converted to lowercase. char first = Character.toLowerCase(text.charAt(0)); char last = Character.toLowerCase(text.charAt(length - 1)); if (Character.isLetter(first) && Character.isLetter(last)) { // Both are letters. if (first == last) { // Remove both first and last character. Sentence shorter = new Sentence(text.substring(1, length - 1)); return shorter.isPalindrome(); } else return false; } else if (!Character.isLetter(last)) { // Remove last character. Sentence shorter = new Sentence(text.substring(0, length - 1)); return shorter.isPalindrome(); } else { // Remove first character. Sentence shorter = new Sentence(text.substring(1)); return shorter.isPalindrome(); } }