-5

I am a beginner in Java programming. I was a making a program to find if the entered word is a pallindrome or not can someone please tell me the logic i should use to make the given program?

Arpit
  • 25
  • 1
  • 11

1 Answers1

0
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.

Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263