public class StringExamples { public static void main(String[] args) { String[] data = { "R2D2" , "Abba" , "\"Madam, I'm Adam!\"" , "say it ain't so, babe!" , "Able was I ere I saw Elba" , "A man, a plan, a canal, Panama!" }; System.out.println( reverse(data[0]) ); System.out.println( getLettersOnly(data[3]) ); System.out.println(); for (String phrase : data) System.out.println(phrase + " is " + (isPalindrome(phrase) ? "" : "NOT ") + "a palindrome" ); } public static String reverse(String s) { if (s == null || s.length() == 0) return s; return reverse( s.substring(1) ) + s.charAt(0) ; } private static String getLettersOnly(String source) { if (source == null || source.length() == 0) return source; char first = source.charAt(0); return (Character.isLetter(first) ? first : "") + getLettersOnly( source.substring(1) ); } private static boolean isSymmetric(String str) { if (str.length() <= 1) return true; if (str.charAt(0) != str.charAt(str.length()-1)) return false; return isSymmetric(str.substring(1, str.length()-1)); } public static boolean isPalindrome(String str) { if (str == null || str.length() <= 1) return true; return isSymmetric( getLettersOnly(str).toLowerCase() ); } }