Java

Java – How to check whether a string is palindrome

In Java, verifying if a string is a palindrome involves examining its characters from both ends and confirming equality. The following Java program illustrates how to perform this palindrome check.

Palindrome.java
public class 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;
    }

    public static void main(String[] args) {
        String str1 = "racecar";
        String str2 = "hello";
        if (isPalindrome(str1)) {
            System.out.println(str1 + " is a palindrome.");
        } else {
            System.out.println(str1 + " is not a palindrome.");
        }
        if (isPalindrome(str2)) {
            System.out.println(str2 + " is a palindrome.");
        } else {
            System.out.println(str2 + " is not a palindrome.");
        }
    }
}

In this code, the isPalindrome() method checks if a given word reads the same backward as forward. It starts with two pointers, left and right, at the beginning and end of the word. The method compares the letters at these positions. If they’re not the same, it says the word isn’t a palindrome. If they match, the pointers move towards each other until they meet in the middle. The main() part shows how to use isPalindrome() to check if two words are palindromes.

Using String Builder:

An alternative method to check for string palindrome involves leveraging the StringBuilder class. This approach employs a case-insensitive comparison and accommodates phrases with spaces and punctuation.

JavaPalindrome.java
public class JavaPalindrome {
    public static boolean isAdvancedPalindrome(String str) {
        StringBuilder reversed = new StringBuilder(str.toLowerCase()).reverse();
        return reversed.toString().equals(str.toLowerCase());
    }

    public static void main(String[] args) {
        String str1 = "Able was I ere I saw Elba";
        String str2 = "Java";

        if (isAdvancedPalindrome(str1)) {
            System.out.println("\"" + str1 + "\" is a palindrome.");
        } else {
            System.out.println("\"" + str1 + "\" is not a palindrome.");
        }

        if (isAdvancedPalindrome(str2)) {
            System.out.println("\"" + str2 + "\" is a palindrome.");
        } else {
            System.out.println("\"" + str2 + "\" is not a palindrome.");
        }
    }
}

The JavaPalindrome class demonstrates this technique, which converts the input string to lowercase, reverses it using a StringBuilder, and compares it to the original string. This provides a more versatile solution for identifying palindromes in diverse contexts.

References:

Happy Learning :)

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

Ask a question or leave a comment