Java program to check palindrome

Here's a Java program to check whether a given string is a palindrome or not:

refer ‮itfigi:ot‬dea.com
import java.util.Scanner;

public class PalindromeChecker {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a string:");
        String input = scanner.nextLine();
        if (isPalindrome(input)) {
            System.out.println(input + " is a palindrome.");
        } else {
            System.out.println(input + " is not a palindrome.");
        }
        scanner.close();
    }
    
    public static boolean isPalindrome(String str) {
        int i = 0;
        int j = str.length() - 1;
        while (i < j) {
            if (str.charAt(i) != str.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}

Explanation:

The main method reads user input using a Scanner object and checks whether the input is a palindrome by calling the isPalindrome method. If the method returns true, it prints a message indicating that the input is a palindrome. Otherwise, it prints a message indicating that the input is not a palindrome.

The isPalindrome method takes a string as input and uses two pointers, i and j, to iterate over the string from both ends. It compares the characters at positions i and j, moving the pointers closer to each other until they meet at the center of the string. If any pair of characters do not match, the method returns false. Otherwise, it returns true to indicate that the input string is a palindrome.

Note that the program assumes that the user input is a valid string. If the input is not a string or is null, the program may produce unexpected results or throw an exception.