import java.util.Scanner; /* * A class to recursively determine if a prhase is a palindrome **/ class P { /** * Recursively determine if string is a palindrome **/ public boolean isPalindrome(String s) { if (s.length()<=1) return true; if (s.charAt(0) == s.charAt(s.length()-1)) { return isPalindrome(s.substring(1, s.length()-1)); } return false; } /** * Driver. * Gets phrases to be checked from user **/ public static void main(String[]args) { Scanner scanner = new Scanner(System.in); P p = new P(); while (true) { System.out.println("Enter the String for check:"); String string = scanner.nextLine(); if (string.length()==0) { System.out.println("DONE"); return; } String sstring=string.replace(" ", ""); if(p.isPalindrome(sstring)) System.out.println("\"" + string + "\" is a palindrome"); else System.out.println("\"" + string + "\" is not a palindrome"); } } public static boolean isPal(String s) { // if length is 0 or 1 then String is palindrome if(s.length() == 0 || s.length() == 1) return true; if(s.charAt(0) == s.charAt(s.length()-1)) return isPal(s.substring(1, s.length()-1)); return false; } }