Java Program to Check Whether a String is a Palindrome
In Java, a string is said to be a palindrome if it reads the same backward as forward. In this article, we will learn how to check if a string is a palindrome in Java.
Example:
The simplest approach to check if a string is a palindrome is by reversing the string and comparing it with the original. Here, to ensure the comparison is case-insensitive, the string is converted to lowercase before the check.
// Java Program to Check if a
// String is a Palindrome
import java.io.*;
public class Palindrome {
// 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
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.
Table of Content
Other Methods to Check a String Palindrome
1. Two Pointer Approach to Check a 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.
Example:
// Java program to check whether a
// string is a Palindrome
// Using two pointing variables
public class Palindrome {
// 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 whilei < 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 a String is Palindrome
The approach is very simple. Just like the two-pointer approach, we will check the first and the last value of the string but this time it will be through recursion.
- We will take two pointers “i” pointing to the start of the string and “j” pointing to the end of the string.
- Base Case: If
i >= j
, the recursion stops, as the string has been fully checked, and it is confirmed to be a palindrome. - Character Comparison: Check if the characters at positions
i
andj
are equal.- If not, return
false
immediately (the string is not a palindrome). - If they match, make a recursive call with the next set of indices (
i + 1
andj - 1
).
- If not, return
- The recursion continues until the base case is satisfied.
If all characters match until the pointers meet or cross, the string is a palindrome. Otherwise, it is not.
Example:
// Java program to check whether a
// string is a Palindrome using recursion
public class Palindrome {
// 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:
- Create a
StringBuilder
object using the input string. - Use the
reverse()
method to reverse the string. - Convert the reversed string back to a
String
usingtoString()
. - Compare the reversed string with the original string using the
equals()
method. - Print whether the string is a palindrome based on the comparison result.
Example:
// Java program to check if a
// string is a palindrome using StringBuilder
public class Palindrome {
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.