Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Pig Latin Program: Write a program that encodes English-language phrase, tokeniz

ID: 3688253 • Letter: P

Question

Pig Latin Program: Write a program that encodes English-language phrase, tokenize the phrase into words with function strtok. To translate each English word into a pig-latin word, place the first letter of the English word at the end of the English word and add the letters "ay". Thus the word "jump" becomes "umpjay", the word "the" become "hetay" and the word "computer" becomes "omputercay". Blacks 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. The sample program execution is as follows: Enter your phrase: hello happy birthday joey Pig-Latin phrase is: ellohay appyhay irthdaybay oeyjay For this question, you are free to define you own functions and use them in the main function. For this question, you need to write your program in file, called question2.c

Explanation / Answer

PigLatinConverter.java


import java.io.*;

/**
* CHEGG EXPERT
*/
public class PigLatinConverter {

/** KEYBOARD inputING. */
public static BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));

/** Main method */
public static void main(String[] args) throws IOException {

//keyboard inputting
System.out.print("Enter sentence: ");
String english_to_latin = buf.readLine();

// Translate and print back out
String latinlanguage = pigLatinlanguage(english_to_latin);
System.out.println(latinlanguage);
}

/**
*/
public static String pigLatinlanguage(String s) {//static method
String latin = "";
int i = 0;
while (i < s.length()) {

// Take care of punctuation and spaces
while (i < s.length() && !isLetter(s.charAt(i))) {
latin = latin + s.charAt(i);
i++;
}

// If there aren't any words left, stop.
if (i >= s.length()) {
break;
}

// Otherwise we're at the beginning of a word.
int begin = i;
while (i < s.length() && isLetter(s.charAt(i))) {
i++;
}

// Now we're at the end of a word, so translate it.
int end = i;
latin = latin + pigWord(s.substring(begin, end));
}
return latin;
}

/**
* Method to test whether a character is a letter or not.
*/
public static boolean isLetter(char c) {
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
}

/**
* Method to translate one word into pig latin.
*/
public static String pigWord(String word) {
int split = firstVowel(word);
return word.substring(split) + "" + word.substring(0, split) + "ay";
}

public static int firstVowel(String word) {//Conveerts words to uppercase
word = word.toLowerCase();
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == 'a' || word.charAt(i) == 'e'
|| word.charAt(i) == 'i' || word.charAt(i) == 'o'
|| word.charAt(i) == 'u') {
return i;
}
}
return 0;
}
}

output

Enter sentence: hello happy birthday joey
ellohay appyhay irthdaybay oeyjay
BUILD SUCCESSFUL (total time: 3 seconds)