Write a program that encodes English-language phrases into pig Latin. To transla
ID: 3839941 • Letter: W
Question
Write a program that encodes English-language phrases into pig Latin. To translate an English word into a pig Latin word, place the first letter of the English word at the end of word and add "ay". For example, "dog" would become "ogday" and cat would become "atcay".
Your program should prompt the user to enter an English sentence and then print the sentence with every word changed to pig latin. (One way to do tis would be to split the sentence into words with the split() method.) For simplicity, there will be no punctuation in the sentences.
Run like:
Enter a string to be translated: the fox jumps over the lazy dog
hetax oxfay umpsjay veroay hetay azylay ogday
Explanation / Answer
PiLatinWithMultipleWords.java
import java.util.Scanner;
public class PiLatinWithMultipleWords {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a string to be translated: ");
String s = scan.nextLine();
String words[] = s.split("\s+");
for(String word: words){
System.out.print(pigLatin(word)+" ");
}
System.out.println();
}
public static String pigLatin (String s){
s = s.toLowerCase();
s = s.substring(1,s.length())+ s.charAt(0) + "ay";
return s;
}
}
Output
Enter a string to be translated: the fox jumps over the lazy dog
hetay oxfay umpsjay veroay hetay azylay ogday
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.