import java.util.Scanner; public class PigLatin { public static void main(String[] args) { String phrase; Scanner input = new Scanner(System.in); System.out.print("Enter a phrase: "); phrase = input.nextLine(); System.out.println(); System.out.println(phrase); System.out.println("in pig latin is"); convertToPigLatin(phrase); } //------------------------------------------------------- // Convert an entire phrase to pig latin and print it out. // Precondition: This method assumes phrase ends with '.'. public static void convertToPigLatin(String phrase) { Scanner inPhrase = new Scanner(phrase); while (inPhrase.hasNext()) { String word = inPhrase.next(); if (word.charAt(word.length()-1) == '.') { System.out.print(pigLatinWord(word.substring(0,word.length()-1)) + "."); } else { System.out.print(pigLatinWord(word) + " "); } } } //-------------------------------------------------------- // Convert one word to pig latin. // Precondition: s is not an empty string // Postcondition: returns a new string that is the pig latin // form of s public static String pigLatinWord(String s) { String pigWord; if (isVowel(s.charAt(0))) { pigWord = s + "-way"; } else if (s.startsWith("th") || s.startsWith("Th")) { // or (s.toUpperCase().startsWith("TH")) pigWord = s.substring(2) + "-" + s.substring(0,2) + "ay"; } else { pigWord = s.substring(1,s.length()) + "-" + s.charAt(0) + "ay"; } return pigWord; } //--------------------------------------------- // Determines whether c is a vowel character // Precondition: c contains a letter // Postcondition: returns true when c is a vowel public static boolean isVowel(char c) { String vowels = "aeiouAEIOU"; return (vowels.indexOf(c) >= 0); // when index of c is not -1, c is a vowel } }