The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function
package testjava.javatest;
import java.util.Scanner;
public class StringPalinRecursion
{
public static void main(String args[])
{
Scanner reader = new Scanner(System.in);
System.out.print("Please enter a String: ");
String input = reader.nextLine();
System.out.printf("Is %s a palindrome? : %b %n", input, isPalindrome(input));
System.out.print("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 inputStr)
{
String reverse = reverse(inputStr);
if(inputStr.equals(reverse))
{
return true;
}
return false;
}
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
Please enter a String: Hello
Is Hello a palindrome? : false
Please enter another String: wow
Is wow a palindrome? : true