Java program to find a pallindrome [closed]

boolean isPalindrome(String input) {
    for (int i=0; i < input.length() / 2; ++i) {
        if (input.charAt(i) != input.charAt(input.length() - i - 1)) {
            return false;
        }
    }

    return true;
}

This solution is self explanatory, the only edge case requiring explanation is what happens for words with an odd number of letters. For an input containing an odd number of letters, the middle element will not be touched by the loop, which is OK because it has no effect on whether the input is a palindrome.

Leave a Comment