/*
 * Example is based on an example from
 * http://java.sun.com/docs/books/tutorial/index.html
 */

import java.io.*;


public class Palindrome {

    public static boolean isPalindrome(String stringToTest) {
	String workingCopy = removeJunk(stringToTest);
        String reversedCopy = reverse(workingCopy);
        return reversedCopy.equalsIgnoreCase(workingCopy);
    }


    protected static String removeJunk(String string) {
        int i, len = string.length();
  	StringBuffer dest = new StringBuffer(len);
	char c;

	for (i = (len - 1); i >= 0; i--) {
	    c = string.charAt(i);
	    if (Character.isLetterOrDigit(c)) {
		dest.append(c);
	    }
	}

        return dest.toString();
    }


    protected static String reverse(String string) {
  	StringBuffer sb = new StringBuffer(string);
        return sb.reverse().toString();
    }


    public static void main(String[] args) throws IOException {

	// set up for interactice input
	BufferedReader consoleIn =
		new BufferedReader(new InputStreamReader(System.in));

	while (true) {
	    System.out.print("\nPalindromeTester-> ");
	    String phrase = consoleIn.readLine();

	    // quit if requested
	    if ((phrase == null) || phrase.equals("quit")) {
		break;
	    }

	    System.out.println("\n--> Testing whether the following "
		    + "phrase is a palindrome:");
	    System.out.println("--> " + phrase);

	    if (isPalindrome(phrase)) {
		System.out.println("--> It IS a palindrome!");
	    } else {
		System.out.println("--> It is NOT a palindrome!");
	    }
	}

    }
}
