import java.util.*; public class PalDemo2 { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); String line; do { System.out.print("Your expression (or return to end): "); line = stdin.nextLine(); if (line.length() == 0) break; if (isPalindrome(line)) System.out.println("That is a palindrome"); else System.out.println("That is not a palindrome."); } while (true); } // Returns true if the given string reads the same // forwards as backwards. // Trivially true for empty or 1-letter strings. public static boolean isPalindrome(String s) { if (s.length() < 2) { return true; // base case } else { char first = s.charAt(0); char last = s.charAt(s.length() - 1); if (first != last) { return false; } // recursive case String middle = s.substring(1, s.length() - 1); return isPalindrome(middle); } } }