// // String convert to Pig Latin // public class pigLatin { static final int MAXWORDS = 512; public static String toString(String source) { int i; // Walk through the string, picking out words and // storing them for later regurgitation in pig latin. char whiteSpace = ' '; int startLoc = 0; int wsLoc = 1; String words[] = new String[MAXWORDS]; int numWords = 0; String newString = ""; while (wsLoc > 0) { wsLoc = source.indexOf(whiteSpace, startLoc); if (wsLoc < 0) { words[numWords++] = source.substring(startLoc); if (numWords == MAXWORDS) { System.out.println("randomWords: too many words in string!"); break; } break; } words[numWords++] = source.substring(startLoc, wsLoc); startLoc = wsLoc+1; } if (numWords == 0) { return ""; } for (i = 0; i < numWords; i++) { // For each word, find the leading consonants and move // them to the end of the word and finish the word by adding // the suffix "ay". int len = words[i].length(); String word = words[i]; StringBuffer newWord = new StringBuffer(len * 2); StringBuffer prefixCons = new StringBuffer(len); boolean foundVowel = false; boolean stopLooking = false; if (word.length() == 0) continue; for (int j = 0; j < len; j++) { if (!stopLooking) foundVowel = isVowel(word.charAt(j)); if (!foundVowel) { // gather up consonants prefixCons.append(word.charAt(j)); } else { // already found the consonants; copy rest of word stopLooking = true; newWord.append(word.charAt(j)); } } // Now, dump the prefix constants and add the suffix. newString += newWord.toString() + prefixCons.toString() + "ay"; if (i < (numWords - 1)) { newString += " "; } } return newString; } static boolean isVowel(char letter) { switch (letter) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'y': return true; } return false; } }