(Pig Latin) Write an application that encodes English-language phrases into pig
ID: 3920848 • Letter: #
Question
(Pig Latin) Write an application that encodes English-language phrases into pig Latin. Pig Latin is a form of coded language. There are many different ways to form pig Latin phrases. For simplicity, use the following algorithm: To form a pig Latin phrase from an English-language phrase, tokenize the phrase into words with String method split. To translate each English word into a pig Latin word, place the first letter of the English word at the end of the word and add the letters “ay.” Thus, the word “jump” becomes “umpjay,” the word “the” becomes “hetay,” and the word “computer” becomes “omputercay.” Blanks between words remain as blanks. Assume the following: The English phrase consists of words separated by blanks, there are no punctuation marks and all words have two or more letters. Method printLatinWord should display each word. Each token is passed to method printLatinWord to print the pig Latin word. Enable the user to input the sentence. Then display the converted sentence.
Explanation / Answer
it can be solved in three steps:
1.read sentences from console.
2.split the sentences in tokens(words).
3.convert each token to pig latin.
I'm using java to demonstrate the procedure.
import java.util.Scanner;
public class Main {
private static void printPigLatinWord(String tok) {
String pigLatinWord = tok.substring(1) + tok.substring(0, 1) + "ay";
System.out.printf("%s ", pigLatinWord);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter sentence: ");
String sentence = scanner.nextLine();
String[] tok = sentence.split(" ");
for (String tok : tok) {
printPigLatinWord(tok);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.