Check Palindrome in Java Using Different Methods

Check Palindrome in Java Using Different Methods

1 min read4K Views Comment
clickHere
Atul
Atul Harsha
Senior Manager Content
Updated on Jan 8, 2023 13:04 IST

Discover various methods for checking if a string is a palindrome in Java. Use recursion and string manipulation to efficiently determine if a string is a palindrome, ignoring spaces, punctuation, and capitalization.

2022_02_j.jpg

In this blog, we will write a program to check palindrome in Java. A palindrome is a word, phrase, number, or other sequence of characters that reads same forward and backward (ignoring spaces, punctuation, and capitalization).

For example, the following words are palindromes:

  • “racecar”
  • “level”
  • “madam”

Explore popular Java Courses

Problem: Write a Java method that takes in a string as a parameter and returns a boolean indicating whether the string is a palindrome or not. The method should ignore spaces, punctuation, and capitalization when determining whether the string is a palindrome. The method should be able to handle strings of any length.

Example:

  • Input: “racecar”
  • Output: true
  • Input: “A man, a plan, a canal, Panama!”
  • Output: true
  • Input: “Hello, world!”
  • Output: false

Method 1: Program to Check Palindrome in Java Using a Loop

  • Iterate through the characters of the string
  • Compare characters at the beginning and end of the string
  • If any pair does not match, return false
  • If the loop completes, return true
 
public static boolean isPalindrome(String str) {
int n = str.length();
// Compare characters from beginning and end
for (int i = 0; i < n/2; i++)
if (str.charAt(i) != str.charAt(n-i-1))
return false;
return true;
}
Copy code

Method 2: Program to Check Palindrome in Java Using a Recursion

  • If the length of the string is 0 or 1, return true (base case)
  • Check the first and last characters of the string
  • If they are different, return false
  • If they are the same, call the function with the substring that excludes the first and last characters
  • Repeat until the base case is reached
 
public static boolean isPalindrome(String str) {
// base case
if (str.length() == 0 || str.length() == 1)
return true;
// check first and last characters
if (str.charAt(0) != str.charAt(str.length()-1))
return false;
// check the remaining string
return isPalindrome(str.substring(1, str.length()-1));
}
Copy code

Method-3: Using Apache Commons Lang library:

  • Use the isPalindrome method from the Apache Commons Lang library to check if the string is a palindrome
  • Include the Apache Commons Lang library in your project to use this method
 
import org.apache.commons.lang3.StringUtils;
public static boolean isPalindrome(String str) {
return StringUtils.isPalindrome(str);
}
Copy code

Method 4: Check Palindrome in Java Using the StringBuilder class

  • Reverse the string using the reverse method of the StringBuilder class
  • Compare the reversed string to the original string using the equals method
  • Return true if they are equal, false otherwise
 
public static boolean isPalindrome(String str) {
StringBuilder sb = new StringBuilder(str);
// reverse the string
sb.reverse();
// check if the reversed string is equal to the original string
return str.equals(sb.toString());
}
Copy code
About the Author
author-image
Atul Harsha
Senior Manager Content

Experienced AI and Machine Learning content creator with a passion for using data to solve real-world challenges. I specialize in Python, SQL, NLP, and Data Visualization. My goal is to make data science engaging an... Read Full Bio