java program to check palindrome string using recursion

Solutions on MaxInterview for java program to check palindrome string using recursion by the best coders in the world

showing results for - "java program to check palindrome string using recursion"
Yannik
30 Jun 2020
1import java.util.Scanner;
2public class RecursivePalindromeJava 
3{
4   // to check if string is palindrome using recursion
5   public static boolean checkPalindrome(String str)
6   {
7      if(str.length() == 0 || str.length() == 1)
8         return true; 
9      if(str.charAt(0) == str.charAt(str.length() - 1))
10         return checkPalindrome(str.substring(1, str.length() - 1));
11      return false;
12   }
13   public static void main(String[]args)
14   {
15      Scanner sc = new Scanner(System.in);
16      System.out.println("Please enter a string : ");
17      String strInput = sc.nextLine();
18      if(checkPalindrome(strInput))
19      {
20         System.out.println(strInput + " is palindrome");
21      }
22      else
23      {
24         System.out.println(strInput + " not a palindrome");
25      }
26      sc.close();
27   }
28}