/**
 *  This program lets the user enter a line of text and then apply various
 *  simple text-processing operations to the string.  The user chooses the
 *  operations from a menu.  The program ends when the user selects the
 *  menu command for exiting from the program.
 *  <p>
 *  Note: This program depends on the non-standard class TextIO.
 */
public class StringSubs {
	
	public static void main(String[] args) {
		
		String lineOfText;  // The line of text entered by the user.

		System.out.println("\n   Welcome to a demonstration of simple string processing.");
		System.out.println("For this program, you can input a line of text and then apply");
		System.out.println("various simple string-processing operations to your input.\n\n");
		
		lineOfText = readUserInput();
		
		while (true) {
			
			int menuChoice;  // The number of the command selected from the menu by the user.
			                 // There are 7 commands in the menu, an menu choice is in the range 1 to 7.

			menuChoice = getUserCommand();
			
			switch (menuChoice) {
			case 1:  // Exit from the program.
				System.out.println("\nOK.  Exiting program.\n");
				System.exit(0);
				break;
			case 2:  // Enter a new string to be processed.
				lineOfText = readUserInput();
				break;
			case 3:  // Count the words in the string.
				int wordCount;
				wordCount = countWords(lineOfText);
				System.out.println("The string contains " + wordCount + " word(s).");
				break;
			case 4:  // Count occurrences of a given character.
				char charToCount;
				int charCount;
				System.out.print("Enter the (non-blank) character to count: ");
				charToCount = TextIO.getlnChar();
				charCount = countChars(lineOfText, charToCount);
				System.out.print("The character '" + charToCount + "' occurs " + charCount + " time(s) in the string.");
				break;
			case 5:  // Count occurrences of all letters.
				printLetterCounts(lineOfText);
				break;
			case 6:  // Test if the string is a palindrome.
				if (isPalindrome(lineOfText) == true)
					System.out.println("The string is a palindrome.");
				else
					System.out.println("The string is not a palindrome.");
				break;
			case 7:  // Use an expurgated version of the string.
				lineOfText = expurgate(lineOfText);
				System.out.println("Now using the string:\n   \"" + lineOfText + "\".");
				break;
			}
			
		} // end while
				
	} // end main()
	
	/**
	 * This method presents a menu to the user and lets the user select one command from the menu.
	 * The menu is a numbered list of commands.  There are 7 commands, numbered 1 to 7.  The user
	 * selects a command by typing in the number, between 1 and 7, of the desired command. 
	 * @return the number of the menu command selected by the user.  The return value is guaranteed to be
	 * one of the integers in the range 1 to 7, inclusive.
	 */
	private static int getUserCommand() {
		int choice;
		System.out.println("\n\nCOMMAND MENU:\n");
		System.out.println("    1.  Exit from the program.");
		System.out.println("    2.  Enter a new string to be processed.");
		System.out.println("    3.  Count the words in the string.");
		System.out.println("    4.  Count occurrences of a given character.");
		System.out.println("    5.  Count occurrences of all letters.");
		System.out.println("    6.  Test if the string is a palindrome.");
		System.out.println("    7.  Use an expurgated version of the string.");
		while (true) {
			System.out.print("\nENTER THE NUMBER OF YOUR CHOICE: ");
			choice = TextIO.getlnInt();
			if (choice >= 1 && choice <= 7)
				break;
		}
		System.out.println();
		return choice;
	}


} // end class StringSubs

