230k views
4 votes
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 this would be to split the sentence into words with the split() method.) For simplicity, there will be no punctuation in the sentences. This is sample run of your program: Enter·a·string·to·be·translated:the·fox·jumps·over·the·lazy·dog↵ hetay·oxfay·umpsjay·veroay·hetay·azylay·ogday↵

User Tolsan
by
4.0k points

1 Answer

7 votes

Answer:

Step-by-step explanation:

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

User Peter Kerr
by
4.1k points