Java Programs to Check Palindrome

1. What is a Palindrome?

A palindrome is a word, phrase, number, or any other sequence of units that may be read the same way in either direction. Generally, if found, commas, separators or other word dividers are ignored, but it is not a mandatory rule.

Similarly, palindrome numbers are those numbers that represent the same number if all digits are reversed (Underscores can be ignored in large numbers such as 1_00_00_001). Underscores in numeric literals are the new addition to Java 7 features.

2. Algorithm

The general steps to check if the specified string or number is a palindrome, follow these steps:

  • Store the original string in a variable.
  • Reverse the String and store it in another variable.
  • Compare both variables.
    • If both strings are equal, then the string is a palindrome.
    • If both strings are unequal, then the string is not a palindrome.

3. Java Palindrome Programs

To check palindrome string, reverse the String characters. Now use equals() or equalsIgnoreCase() method to verify if the given string was palindrome or not.

3.1. By Reversing the String

The StringBuilder.reverse() and apache Common Lang’s StringUtils.reverse() methods can reverse the method in a single line.

public static boolean isPalindromeUsingStringBuilder(String originalString) {

  String reverse = new StringBuilder(originalString).reverse().toString();
  return originalString.equals(reverse);
}

public static boolean isPalindromeUsingStringUtils(String originalString) {

  String reverse = StringUtils.reverse(originalString);
  return originalString.equals(reverse);
}

3.2. Using For-Loop

Use for loop to get the reverse string by iterating over string characters from the last index using charAt() method and create a new string.

Use this approach only when we are checking string palindrome without using inbuilt reverse methods.

public static boolean isPalindromeUsingForLoop(String originalString) {

  String reverse = "";

  for (int i = originalString.length() - 1; i >= 0; i--) {
    reverse = reverse + originalString.charAt(i);
  }
  return originalString.equals(reverse);
}

3.3 Using Recursion

In recursion, rather than creating a new String, which is the reverse of the original String, we can compare the characters of the given string from the beginning and the end.

If the characters at the corresponding positions from beginning and end are the same, then the string is a palindrome.

public static boolean isPalindromeUsingRecursion(String string) {

  if (string.length() == 0 || string.length() == 1) {
    return true;
  }
  if (string.charAt(0) == string.charAt(string.length() - 1)) {
    return isPalindromeUsingRecursion(string.substring(1, string.length() - 1));
  }
  return false;
}

4. Demo

Let us run all the above-discussed approaches and test their output.

String input1 = "howtodoinjava";
String input2 = "naman";

isPalindromeUsingStringBuilder(input1); //false
isPalindromeUsingStringBuilder(input2); //true

isPalindromeUsingStringUtils(input1); //false
isPalindromeUsingStringUtils(input2); //true

isPalindromeUsingForLoop(input1); //false
isPalindromeUsingForLoop(input2); //true

isPalindromeUsingRecursion(input1); //false
isPalindromeUsingRecursion(input1); //true

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.