/home/wpollock1/public_html/Java/TxtCrypt/PigLatin.java

import java.io.*;

/** Java utility class to convert text into Pig-latin.
 * The main task is performed by the <code>encrypt</code> method.
 * <p>
 * Pig-latin words are formed by shifting leading consonants of
 * a word to the end, and adding "ay".  If the word begins with
 * a vowel, just add "way" (no shifting).  So "Java" becomes
 * "Avajay" while "out" becomes "outway".  There are extra rules
 * for dealing with "u" and "y".  For simplicity this code treats
 * hyphens and apostrophes as consonants, so words such as "I'll"
 * and "Q-Tip" don't come out quite right.

 * @author Wayne Pollock, Tampa, FL USA
 * @version 1.0
 */

public class PigLatin
{
private PigLatin () {}  // Nobody can construct objects of this class.

/** This main method is used only as a test driver for the
 * {@link #encrypt(String) encrypt(String)} method.
 */
public static void main ( String [] args )
{
    String testString = "Java is very simple!";
    System.out.println( "Testing PigLatin.encrypt() method:" );
    String result = encrypt( testString );
    System.out.println( "Input:  " + testString );
    System.out.println( "Output: " + result );
}

/** Encrypts text into Pig-latin.
 *  @param text The plain-text to be encrypted.
 *  @return The encrypted text.
 */
public static String encrypt ( String text )
{
   if ( text.length() == 0 )
      return "";
   StringReader sr = new StringReader( text );
   StringWriter sw = new StringWriter();

   for ( ;; )
   {
      String word = getNextWord( sr, sw );
      if ( word.equals("") )
          break;  // End of text reached, all done.

      // Write the "pig latin"-ized word:
      sw.write( xlateWord(word) );
   }
   try
   {  sr.close();
      sw.close();
   } catch ( Exception ignored ) {}
   return sw.toString();
}


// getNextWord is a kind of String tokenizer, but it correctly
// handles punctuation symbols:
private static String getNextWord (StringReader sr, StringWriter sw)
{
   char c = ' ';  // Initialize to avoid annoying javac warnings!
   int ch = 0;

   // Read the input, writting the characters read, until
   // the start of a word is found:

   try
   {
      while ( ( ch=sr.read() ) !=  -1 )
      {
         c = (char) ch;
         if ( Character.isLetter(c) ) break;
         sw.write( c );
      }
   } catch ( IOException ignored ) {}

   if ( ch == -1 )  return "";   // All done; return an empty string.

   // Now collect all the letters in the word, and return it:
   StringBuffer word = new StringBuffer();
   do
   {  try
      {  word.append( c );
         sr.mark(1);
         ch = sr.read();
         c = (char) ch;
      } catch (IOException ignored) {}
   } while ( (ch !=  -1) && ( c == '-' || c == '\'' || Character.isLetter(c) ) );

   if ( ch != -1 ) try {
      sr.reset();  // rewind to the last mark, i.e., back up one character.
   }  catch ( IOException ignored ) {}

   return word.toString();
}


// xlateWord implements the Pig Latin rules and translates its arg.
private static String xlateWord ( String inWord )
{
   StringBuffer word = new StringBuffer( inWord );
   char let = word.charAt( 0 );
   boolean isUpCase = Character.isUpperCase( let );
   boolean allCaps = inWord.equals( inWord.toUpperCase() );
   boolean containsHyphen = ( inWord.indexOf( '-' ) != -1 );
   let = Character.toLowerCase( let );

   // Words not begining with a consonant have "way" appended:

   if ( let == 'a' || let == 'e' || let == 'i' || let == 'o' || let == 'u'
        || ( ! Character.isLetter(let) )  )
   {
      if ( allCaps )
         word.append( "WAY" );
      else
         word.append( "way" );
      return word.toString();
   }

   // Words begining with a consonant have the leading consonants
   // (including a "u" preceeded by a "q") shifted to the end,
   // and the suffix "ay" is then added:

   int i;
   boolean qFlag = false;     // When true shows the previous letter was a 'q'.
   if ( ! allCaps )
      word.setCharAt( 0, let );  // Replace original letter with lower-case.
   StringBuffer leadingConsonants = new StringBuffer();

   loop:
   for ( i=0; i<word.length(); ++i )
   {
      let = word.charAt( i );
      switch ( Character.toLowerCase(let) )
      {
         case 'a':  case 'e':  case 'i':  case 'o':  case 'y':
            break loop;

         case '-':              // Turn "q-tip" into "tip-qay".
            word.append( '-' );
            ++i;
            break loop;

         case 'q':
            qFlag = true;
            leadingConsonants.append( let );
            break;

         case 'u':
            if ( qFlag )
            {  qFlag = false;
               leadingConsonants.append( let );
               break;
            } else
            {  break loop;
            }

         default:
            qFlag = false;
            leadingConsonants.append( let );
            break;
      }
   }

   if ( allCaps )
      leadingConsonants.append( "AY" );
   else
      leadingConsonants.append( "ay" );

   if ( i >= word.length() )  // Then the word has no vowels!
      return leadingConsonants.toString();

   if ( isUpCase )   // If original word was capitalized, so is result.
   {  let = word.charAt( i );
      word.setCharAt( i, Character.toUpperCase(let) );
   }

   StringBuffer outWord = new StringBuffer();
   outWord.append( word.toString().substring(i) );
   outWord.append( leadingConsonants.toString() );
   return outWord.toString();
}

} // End of class PigLatin