import javax.swing.JOptionPane; public class PalindromeTester { //Process a series of input Strings //Report whether each String is a palindrome public static void main(String[] args) { String input; do { input = JOptionPane.showInputDialog(null, "Any String"); if (input != null) System.out.println(input + " IS " + (isPalindrome(input) ? "" : " NOT ") + "a Palindrome" ); } while (input != null); } //Return true iff parameter str is a Palindrome public static boolean isPalindrome(String str) { String testString = lettersOnly(str); return reverse(testString).equalsIgnoreCase(testString); //return isSymmetric( lettersOnly(str).toLowerCase() ); } //Return a String containing only the letters of the parameter // in the same order as they occur in the parameter public static String lettersOnly(String str) { if (str.length() == 0) return ""; char firstChar = str.charAt(0); String remainder = str.substring(1) ; return (Character.isLetter(firstChar) ? firstChar : "") + lettersOnly( remainder ); } //Return a String which is the reverse of the parameter public static String reverse(String str) { if (str.length() <=1) return str; return reverse(str.substring(1)) + str.charAt(0); } //Return true iff the parameter str reads the same // backwards as forwards private static boolean isSymmetric(String str) { if (str.length() <= 1) return true; int endIndex = str.length()-1; if ( str.charAt(0) != str.charAt(endIndex) ) return false; return isSymmetric( str.substring(1, endIndex) ); } }