Palindrome string in java using recursion

Hi!! Welcome to flower brackets blog. In this post let’s learn palindrome string in java using recursion.

Recursion means a function calling itself is called recursion. In this java program initially “checkPalindrome” method with String parameter “str” is created.

Also read – palindrome program in java using library methods

This method first checks if user entered string length is 0 or 1 using if statement. Here if string length is equal to 0 or 1 then string is palindrome.

If not, in the next if statement first and last character of string is checked. If first and last character of string are same then procedure is repeated for substring without first and last character.

This procedure is continued till string condition is not true. Here’s the java program to check palindrome string using recursion,

import java.util.Scanner;

public class RecursivePalindromeJava 
public static boolean checkPalindrome(String str)
if(str.length() == 0 || str.length() == 1)
return true;
if(str.charAt(0) == str.charAt(str.length() - 1))
return checkPalindrome(str.substring(1, str.length() - 1));
return false;
   public static void main(String[]args)
Scanner sc = new Scanner(;
System.out.println("Please enter a string : ");
String strInput = sc.nextLine();
System.out.println(strInput + " is palindrome");
System.out.println(strInput + " not a palindrome");


Please enter a string : mom
mom is palindrome

Please enter a string : flower
flower not a palindrome

Related Posts