A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-program-to-check-whether-a-string-is-a-palindrome/ below:

Java Program to Check Whether a String is a Palindrome

Java Program to Check Whether a String is a Palindrome

Last Updated : 12 Jul, 2025

A string in Java can be called a palindrome if we read it from forward or backward, it appears the same or in other words, we can say if we reverse a string and it is identical to the original string for example we have a string s = "jahaj " and when we reverse it s = "jahaj"(reversed) so they look identical so we can say that "jahaj" is a palindrome string. In this article, we will go through different approaches to check if a string is a palindrome in Java. 

Example Input/Output:

Input: s = "level"
Output: True

Input: s = "Geeks"
Output: True

Input s = "G"
Output: True.

Brute Force Approach

The brute force or naive approach to check if a string is a palindrome is by reversing the string, and then we can compare it with the original. Here, to ensure the comparison is case-insensitive, the string is converted to lowercase before the check. For example, if we do not convert the string to lowercase so it won't work with the different cases, such as Level and level, it will give false, but it is a palindrome.

Algorithm:

Example: Java program to check whether a string is a palindrome by comparing it with its reversed string.

Java
// Java Program to Check if a 
// String is a Palindrome
import java.io.*;

public class Geeks
{

    // Method to check if a string is a palindrome
    public static boolean isPalindrome(String s) {
      
        // Convert string to lowercase for 
        // case-insensitive comparison
        s = s.toLowerCase();

        // Reverse the string
        String rev = "";
        for (int i = s.length() - 1; i >= 0; i--) {
            rev = rev + s.charAt(i);
        }

        // Compare the original string with 
        // the reversed string
        return s.equals(rev);
    }

    public static void main(String[] args) {
      
        // Input string
        String s = "level";

        // Check if the string is a palindrome
        boolean res = isPalindrome(s);

        // Print the result with enhanced output
        if (res) {
            System.out.println('"' + s + '"' + " is a palindrome.");
        } else {
            System.out.println('"' + s + '"' + " is not a palindrome.");
        }
        }
    }

Output
"level" is a palindrome.

Explanation: In the above example, It checks if the given string is palindrome by converting it to lowercase, reversing it, and then comparing the reversed string with the original.

Other Methods to Check a String Palindrome 1. Two Pointer Approach to Check if incrementa String Is Palindrome

This approach uses two pointers, one starting at the beginning "i" and the other at the end "j" of the string. By comparing characters at these pointers and moving them inward, we can determine if the string is a palindrome or not.

Algorithm:

Example:

Java
// Java program to check whether a
// string is a Palindrome
// Using two pointing variables
public class Geeks
{
    // Method to check if a string is a palindrome
    public static boolean isPalindrome(String s) {
        int i = 0, j = s.length() - 1;

        // Compare characters while i < j
        while (i < j) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;      
            }
            i++;
            j--;
        }
        return true;      
    }

    public static void main(String[] args) {
      
        // Input strings
        String s1 = "geeks";
        String s2 = "Racecar";

        // Convert strings to lowercase for 
        // case-insensitive comparison
        s1 = s1.toLowerCase();
        s2 = s2.toLowerCase();

        // Check and print results for s1
        if (isPalindrome(s1)) {
            System.out.println("\"" + s1 + "\" is a palindrome.");
        } else {
            System.out.println("\"" + s1 + "\" is not a palindrome.");
        }

        // Check and print results for s2
        if (isPalindrome(s2)) {
            System.out.println("\"" + s2 + "\" is a palindrome.");
        } else {
            System.out.println("\"" + s2 + "\" is not a palindrome.");
        }
    }
}

Output
"geeks" is not a palindrome.
"racecar" is a palindrome.

Explanation: In this example, the isPalindrome method checks for mismatched characters while i < j and returns false if found. The main method converts input strings to lowercase and prints whether they are palindrome.

2. Recursive Approach to Check if a String is Palindrome

Now for recursion we are using the same approach as we used in the two pointer, here we create two pointer I from start and j from the end in the recursion. and Increaement the i by one and decrement j by 1, if the characters at the index i not match the character present at index j then it is not a palidrome if it the base condition hit i=j and

Algorithm:

If all characters match until the pointers meet or cross, the string is a palindrome. Otherwise, it is not.

Example:

Java
// Java program to check whether a
// string is a Palindrome using recursion
public class Geeks
{
    // Recursive method to check 
    // if a string is a palindrome
    public static boolean isPalindrome(int i, int j, String s) {
      
        // If pointers have crossed, 
        // it's a palindrome
        if (i >= j) {
            return true;
        }

        // If characters at i and j are not the same, 
        // return false
        if (s.charAt(i) != s.charAt(j)) {
            return false;
        }

        // Recursive call for the 
        //next pair of pointers
        return isPalindrome(i + 1, j - 1, s);
    }

    // Overloaded method to simplify the call
    public static boolean isPalindrome(String s) {
        return isPalindrome(0, s.length() - 1, s);
    }

    public static void main(String[] args) {
      
        // Input strings
        String s1 = "geeks";
        String s2 = "Racecar";

        // Convert strings to lowercase for 
        // case-insensitive comparison
        s1 = s1.toLowerCase();
        s2 = s2.toLowerCase();

        // Check and print results for s1
        if (isPalindrome(s1)) {
            System.out.println("\"" + s1 + "\" is a palindrome.");
        } else {
            System.out.println("\"" + s1 + "\" is not a palindrome.");
        }

        // Check and print results for s2
        if (isPalindrome(s2)) {
            System.out.println("\"" + s2 + "\" is a palindrome.");
        } else {
            System.out.println("\"" + s2 + "\" is not a palindrome.");
        }
    }
}

Output
"geeks" is not a palindrome.
"racecar" is a palindrome.
3. StringBuilder Approach to Check a String is Palindrome

In this approach, we will use the StringBuilder class to reverse a string and compare it with the original string. Below are the steps:

Example:

Java
// Java program to check if a 
// string is a palindrome using StringBuilder
public class Geeks
{
    public static void main(String[] args) {
      
        // Input string
        String s = "GeeksForGeeks";

        // Create a StringBuilder object 
        //with the original string
        StringBuilder s1 = new StringBuilder(s);

        // Reverse the string 
        // using the reverse() method
        s1.reverse();

        // Compare the reversed string 
        // with the original string
        if (s.equals(s1.toString())) {
            System.out.println("\"" + s + "\" is a palindrome string.");
        } else {
            System.out.println("\"" + s + "\" is not a palindrome string.");
        }
    }
}

Output
"GeeksForGeeks" is not a palindrome string.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4