import java.io.*;
import java.util.*;
public class Pig {
    public static void main(String[] argv) throws IOException {
        System.out.print("Enter a word to convert to pig latin: ");
        String input = new BufferedReader(new InputStreamReader(System.in)).readLine();
        StringTokenizer st = new StringTokenizer(input);
        String output = "";
        while(st.hasMoreTokens()) {
            if(output.length() > 0) output += " ";
            output += convertWord(st.nextToken());
        }
        System.out.println("Output: " + output);
    }
    
    public static String convertWord(String word) {
        int p = indexOfVowel(word);
        int end = indexOfLastLetter(word) + 1;
        if(p == -1) { // no vowel
            return word;
        } else if(p == 0) {
            return word.substring(0,end) + "way" + word.substring(end);
        } else {
            return word.substring(p,end) + word.substring(0,p) + "ay" + word.substring(end);
        }
    }
    
    public static int indexOfLastLetter(String word) {
        for(int i=word.length()-1;i>=0;i--)
            if(Character.isLetterOrDigit(word.charAt(i)))
                return i;
        return -1;
    }
    
    public static int indexOfVowel(String s) {
        String[] vowels = new String[]{ "a","e","i","o","u" };
        s = s.toLowerCase();
        int first = -1;
        for(int i=0;i<vowels.length;i++) {
            int p = s.indexOf(vowels[i]);
            if(p >= 0 && (first < 0 || p < first)) first = p;
        }
        return first;
    }
}
