/* * Projlet6_palindromes.java * * Created on August 17, 2003, 9:53 PM */ /** Projlet #6 programming solution for palindrome problems */ public class Projlet6_palindromes { /*************************** 2a ******************************/ /** See if the string is an exact palindrome. Case and non-letter * characters must match exactly. */ public static boolean isExactPalindrome(String message) { if (message == null) { return false; } if (message.length() <= 0) { //these are real base cases return true; } return isExactPalindrome(message, 0, message.length()-1); } /** Check whether the portion of the string between indices lo and hi * (inclusive) is an exact palindrome. * Preconditions: message is not null. lo and hi are in bounds. */ private static boolean isExactPalindrome(String message, int lo, int hi) { assert lo >= 0 && hi >= lo && hi < message.length(): lo + " " + hi; if (lo == hi) { //base case -- a string of length 1 return true; } if (message.charAt(lo) != message.charAt(hi)) { //base case -- the outer characters don't match return false; } if (hi == lo+1) { return true; //base case -- two-letter string of duplicate chars. } return isExactPalindrome(message, lo+1, hi-1); } /************************ 2b **********************************************/ /** See if the string is a palindrome, disregarding case and ignoring all non-letter characters. */ public static boolean isPalindrome(String message) { if (message == null) { return false; } if (message.length() <= 1) { //these are real base cases return true; } return isPalindrome(message, 0, message.length()-1); } /** Check whether the portion of the string between indices lo and hi * (inclusive) is a (general) palindrome. * Preconditions: message is not null. lo and hi are in bounds. */ private static boolean isPalindrome(String message, int lo, int hi) { assert lo >= 0 && hi >= lo && hi < message.length(): lo + " " + hi; if (lo == hi) { //base case -- a string of length 1 return true; } char loChar = message.charAt(lo); if (!Character.isLetter(loChar)) { return isPalindrome(message, lo+1, hi);//skip non-letter character } char hiChar = message.charAt(hi); if (!Character.isLetter(hiChar)) { return isPalindrome(message, lo, hi-1);//skip non-letter character } //we now are sure there are two letter characters to be compared if (Character.toLowerCase(loChar) != Character.toLowerCase(hiChar)) { //base case -- the outer characters don't match return false; } //The outer chars are the same (except perhaps for case) if (hi == lo+1) { return true; //base case -- two-letter string of duplicate chars. } return isPalindrome(message, lo+1, hi-1); } /** For testing. * @param args the command line arguments (ignored). */ public static void main(String[] args) { assert isExactPalindrome(""); assert !isExactPalindrome(null); assert isExactPalindrome("X"); assert isExactPalindrome("aa"); assert !isExactPalindrome("Aa"); assert isExactPalindrome("ABA"); assert isExactPalindrome("ABBA"); assert isExactPalindrome("AB BA"); assert !isExactPalindrome("AB CBA"); assert !isExactPalindrome(" ABBA"); assert isPalindrome(""); assert !isPalindrome(null); assert isPalindrome("X"); assert isPalindrome("aa"); assert isPalindrome("Aa"); assert isPalindrome("ABA"); assert isPalindrome("ABBA"); assert isPalindrome("AB BA"); assert isPalindrome("AB CBA"); assert isPalindrome(" ABBA"); assert isPalindrome("Madam, I'm Adam!"); assert !isExactPalindrome("Madam, I'm Adam!"); assert !isPalindrome("Madame, I'm Adam."); assert !isPalindrome("Madam, I am Adam."); assert isPalindrome(" ,,..,,..a!!"); assert isPalindrome(" a[]{} ,,..,,..a!!"); assert isPalindrome(" aa[]{} 1234567890!@#$%^&*() ,,..,,..a!!"); System.out.println("End palindromes test."); } }