/* * IxNay : Convert text into IxNay (Pig Latin). * * Parke Godfrey 2009-11-16 */ import java.io.PrintStream; import java.util.Scanner; public class IxNay { public static void main(String[] args) { PrintStream output = System.out; Scanner input = new Scanner(System.in); final String AY = "ay"; final String YAY = "Yay"; int start; // Beginning of substring. int end; // Ending of substring. int front; // Split between starting consonants and rest in a word. output.println("Enter a sentence:"); while (input.hasNext()) { String sentence = input.nextLine(); start = 0; end = 0; while (end < sentence.length()) { // Find a non-word stretch and print it out. start = end; while (end < sentence.length() && sentence.substring(start, end + 1) .matches("[^a-zA-Z]+")) { end++; } output.print(sentence.substring(start, end)); // Find a word stretch. start = end; while (end < sentence.length() && sentence.substring(start, end + 1) .matches("[a-zA-Z]+")) { end++; } // Find initial consonant stretch within the word. front = start; while (front < end && sentence.substring(start, front + 1) .matches("[^aeiouAEIOU]+")) { front++; } // Print the word in IxNay appropriately. if (front - start > 0) { output.print(sentence.substring(front, end) + sentence.substring(start, front) .toUpperCase() + AY); } else if (end - start > 0) { output.print(sentence.substring(start, end) + YAY); } } output.println(""); output.println("Enter a sentence:"); } output.println("Done."); } }