Java Program to Check Palindrome

What is a Palindrome?

A palindrome is a word, phrase, number, or other sequence of characters that reads the same backward as forward. Some examples of palindromes include “racecar,” “level,” and “madam.” Palindromes are often used in puzzles, word games, and literature.

To determine whether a given string is a palindrome, you can compare the characters at the beginning and end of the string. If the characters are the same, move inward and continue the comparison. If all the characters match, the string is a palindrome. If any two characters don’t match, the string is not a palindrome.

In programming, you can use a loop to compare the characters of a string and determine whether it is a palindrome or not. You can also use built-in functions or methods to reverse a string and compare it to the original string.

Palindromes have been studied in various fields, including mathematics, computer science, and linguistics. They have applications in cryptography, data compression, and error-correction codes.

Java Code Example to Find Palindrome String

Here is an example of a Java program to check whether a given string is a palindrome or not. The program takes a string as input, and outputs whether or not the string is a palindrome:

import java.util.Scanner;

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

Output:

Enter a string: racecar
racecar is a palindrome.

In this program, the main method prompts the user to enter a string and reads the input using a Scanner. It then calls the isPalindrome method to determine if the input string is a palindrome or not. If the string is a palindrome, it prints a message indicating that it is. If the string is not a palindrome, it prints a message indicating that it is not.

The isPalindrome method checks whether a given string is a palindrome or not. It uses two pointers, left and right, to compare the characters at the beginning and end of the string. If the characters at left and right are not equal, the method immediately returns false, indicating that the string is not a palindrome. If the characters are equal, the method continues iterating through the string until the pointers meet in the middle. If the method reaches the end of the loop without returning false, it returns true, indicating that the string is a palindrome.

Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author