import java.io.*;
import java.util.*;

/** DemoTransliateEnglishConsole -
* program to translate English into Pig Latin -- console version.<p>
*
* This demo program and the next (<code>DemoKeyEvents</code>) serve
* to contrast <i>sequential programming</i> with <i>event-driven
* programming</i>.  Our focus here is on sequential progamming.<p>
*
* <b>Sequential programming</b>.
* All console programs use a programming strategy known as
* <i>sequential programming</i>.  In sequential programs, the
* program is "under control".  In other words, the user is required
* to synchronize with the program, rather than the program
* synchronizing with the user.  Statements are executed
* one after another in a pre-determined
* sequence.  When the program requires input, the user is prompted
* to provide input.  Upon receiving input, the program processes
* the input and reacts accordingly, such as outputing
* results to the console or a file.  Following this, the
* program may terminate or perhaps request more input.
* <p>
*
* Examples of sequential programming are command-line interpreters
* such as the unix or DOS shells, or LISP interpreters.  Console
* applications, such as this program, are also examples of
* sequential programming,
* <p>
*
* Screen snap...<br>
* <center><img src = "DemoTranslateEnglishConsole-1.gif"></center><p>
*
* @see <a href="DemoTranslateEnglishConsole.java">source code</a>
* @author Scott MacKenzie, 2003
*/
public class DemoTranslateEnglishConsole
{
   public static void main(String[] args) throws IOException
   {
      final String BANNER = "Translate English to Pig Latin";
      final String PROMPT = "Enter some text\n" +
                            "(terminate with CTRL-Z (Windows) or CTRL-D (unix))";

      BufferedReader stdin =
         new BufferedReader(new InputStreamReader(System.in));

      // output banner
      System.out.println(BANNER);

      // prompt the user
      System.out.println(PROMPT);

      String phrase;
      while (true)
      {
         // get input
         phrase = stdin.readLine();

         // see if we're done
         if (phrase == null)
            break;

         // 'parse and evaluate'
         String newPhrase = translatePhrase(phrase);

         // output results
         System.out.println(newPhrase);
      }
   }

   public static String translatePhrase(String s)
   {
      StringTokenizer st = new StringTokenizer(s);

      String translation = "";
      while (st.hasMoreTokens())
      {
         String word = st.nextToken();
         String pigLatinWord = translateWord(word);
         translation += pigLatinWord + " ";
      }
      return translation;
   }

   private static String translateWord(String s)
   {
      String s1 = s.length() > 1 ? s.substring(1, s.length()) : "";
      String s2 = s.length() > 0 ? s.substring(0, 1) : "";
      String s3 = "ay";
      return s1 + s2 + s3;
   }
}


