In this tutorial, you will learn how to check if a string is a palindrome in Java using Recursion. A String is nothing but a collection of characters like “Java,” and String literals are encoded in double-quotes in Java. A String is said to be a palindrome if the reverse of String is equal to itself like “aba” is a palindrome because the opposite of “aba” is also “aba”, but “abc” is not a palindrome because the reverse of “abc” is “cba” which is not equal. Recursion means solving a problem by writing a function which calls itself. In order to check if String is a palindrome in Java, we need a function that can reverse the String.
Once you have the original and reversed String, all you need to do is check if they are equal to each other or not. If they are equal then String is palindrome or not. You can write this reverse() function by using either for loop or by using Recursion.
If you remember, I already shared the logic of reversing String in my earlier post, how to reverse String in Java using Iteration and Recursion. Here we will use the same logic to check if String is palindrome or not.
This is a beautiful course, which contains lots of natural and medium difficulty level coding problems, which will not only help you to prepare for an interview but also develop your programming logic and Data Structure, and Algorithms skills. It’s also very affordable and you can buy in just $10 on many Udemy sales which happen every now and then.
Java Program to check if String is Palindrome Using Recursion
Here is our Java program, which checks if a given String is palindrome or not. The program is simple, and here are steps to find palindrome String :
1) Reverse the given String
2) Check if the reverse of String is equal to itself; if yes, then given String is a palindrome.
In our solution, we have a static method isPalindromeString(String text), which accepts a String. It then calls the reverse(String text) method to reverse this String. This method uses Recursion to reverse String. This function first checks if the given String is null or empty, if yes then it returns the same String because they don’t require to be reversed.
After this validation, it extracts the last character of String and passes rest or String using substring() method to this method itself, hence the recursive solution. The validation also servers as base case because, after every step, String keeps getting reduced, and eventually it will become empty, there your function will stop Recursion and will use String concatenation to concatenate all character in reverse order. Finally, this method returns the reverse of String.
When the call to reverse() returns back, isPalindromeString(String text) uses the equals() method to check if the reverse of String is equal to the original String or not, if yes then it returns true, which also means String is a palindrome.

How to check if String is Palindrome in Java using Recursion
Here is the complete Java program to check if the given String is palindrome or not. In this program, we have used Recursion to first reverse the String and then check if both original and reversed String is the same or not.
package test; /** * Java program to show you how to check if a String is palindrome or not. * An String is said to be palindrome if it is equal to itself after reversing. * In this program, you will learn how to check * if a string is a palindrome in java using recursion * and for loop both. * * @author Javin */ public class PalindromeTest { public static void main(String args[]) { System.out.println("Is aaa palindrom?: " + isPalindromString("aaa")); System.out.println("Is abc palindrom?: " + isPalindromString("abc")); System.out.println("Is bbbb palindrom?: " + isPalindromString("bbbb")); System.out.println("Is defg palindrom?: " + isPalindromString("defg")); } /** * Java method to check if given String is Palindrome * @param text * @return true if text is palindrome, otherwise false */ public static boolean isPalindromString(String text){ String reverse = reverse(text); if(text.equals(reverse)){ return true; } return false; } /** * Java method to reverse String using recursion * @param input * @return reversed String of input */ public static String reverse(String input){ if(input == null || input.isEmpty()){ return input; } return input.charAt(input.length()- 1) + reverse(input.substring(0, input.length() - 1)); } } Output Is aaa palindrom?: true Is abc palindrom?: false Is bbbb palindrom?: true Is defg palindrom?: false
How to check if String is Palindrome using StringBuffer and For loop
You can also solve this problem by retrieving the character array from String using the toCharArray() and using a for loop and StringBuffer. All you need to do is iterate through the character array from end to start i.e. from the last index to the first index and append those characters into the StringBuffer object. Once this is done, just call the toString() method of StringBuffer, it’s your reversed String.
Here is how your code will look like :
import java.util.Scanner; /** * How to check if String is palindrome in Java * using StringBuffer and for loop. * * @author java67 */ public class Palindrome{ public static void main(String args[]) { Scanner reader = new Scanner(System.in); System.out.println("Please enter a String"); String input = reader.nextLine(); System.out.printf("Is %s a palindrome? : %b %n", input, isPalindrome(input)); System.out.println("Please enter another String"); input = reader.nextLine(); System.out.printf("Is %s a palindrome? : %b %n", input, isPalindrome(input)); reader.close(); } public static boolean isPalindrome(String input) { if (input == null || input.isEmpty()) { return true; } char[] array = input.toCharArray(); StringBuilder sb = new StringBuilder(input.length()); for (int i = input.length() - 1; i >= 0; i--) { sb.append(array[i]); } String reverseOfString = sb.toString(); return input.equals(reverseOfString); } }
That’s all about how to check for palindrome in Java. You have learned how to find if a given String is palindrome using Recursion as well by using StringBuffer and for a loop. More importantly, you have done it by developing your own logic and writing your own code i.e. not taking help from a third-party library.
Pingback: JAVA TOP 50 PROGRAMMING QUESTIONS - GRAD JOB OPENINGS