import java.util.*;

/* This program determines whether or not a string is a palendrome
 *  
 * Adam Tran
 * 10/28/08
 * 
 */



class TestPalindrome {
	
	/* This class dumps valid characters of a palindrome into a string and tests to see if it is a palindrome
	 * 
	 */
	
	//No instance variables
	
	//Default constructor
	
	public TestPalindrome(){}
	
	//Methods
	
	/* Dump all valid characters into a string, lower case
	 * 
	 */
	
	private String dumpString(String input){
		int length = input.length(); //Length of string
		String output = "";
		input = input.toLowerCase(); //All lower case
		for(int character = 0; character < length; character = character + 1){
			if ((input.charAt(character) >= 97 && input.charAt(character) <= 122) //Valid characters a-z
					|| (input.charAt(character) >= 48 && input.charAt(character) <= 57)){ //Valid numbers 0-9
					output = output + input.charAt(character); //Concantinate single character to string
			}
		}
		return output;
	}
	
	/* Reverse string by concantinating characters in reverse order
	 * 
	 */
	
	private String reverseString(String input){
		int length = input.length(); //Length
		String output = "";
		for(int character = length - 1; character >= 0; character = character - 1){ //Counting backward
			output = output + input.charAt(character); //Add character to string
		}
		return output;
	}
	
	/* Return a boolean for whether or not a string is a palindrome
	 * 
	 */
	
	public boolean testString(String input){
		String formattedString = this.dumpString(input); //Dump string
		if (input.length() > 1){
			if (formattedString.equals(reverseString(formattedString))){ //Compare to reversed string
				return true;
			} else
				return false;
		} else
			return false;
	}
	
	/* Return a string based on whether or not a string is a palindrome
	 * 
	 */
	
	public String test(String input){
		if (this.testString(input))
			return "Yes, the string you entered is a palindrome.";
		else
			return "No, the string you entered is NOT a palindrome.";	
	}
	
}


public class Palindrome {

	public static void main(String[] args) {
		
		// Palindrome tester and scanner objects
		Scanner reader = new Scanner(System.in);
		TestPalindrome tester = new TestPalindrome();
		
		//Initiate input string
		String inputString = "";
	
		//Intro
		System.out.println("Welcome to the Palindrome Program!");
		
		//Sentinel-controlled loop, exit on single "q"
		while (true){
				
			System.out.print("\nEnter a string: ");
			
			inputString = reader.nextLine();
			
			//Break out of loop			
			if (inputString.equalsIgnoreCase("q")){
				System.out.println("\nGoodbye!");
				break;
			}
			
			//Return a string for whether or not a string is a palindrome
			System.out.println(tester.test(inputString));
		}
	}
}

