78.6k views
5 votes
Pig Latin is a nonsense language. To create a word in pig Latin, you remove the first letter and then add the first letter and "ay" at the end of the ford. For example, "dog" becomes "ogday" and "cat" becomes "atcay". Write a program named PigLatin that allows the user to enter a word and display’s the pig Latin version. For words which begin with vowel sounds or silent letter, one just adds "yay" to the end. Examples are:  "eat" → "eatyay"  "omelet" → "omeletyay"  "are" → "areyay" Another less common way some speakers may use a different ending for words starting with vowels is adding "way" (or "wa") to the end. Examples are:  "egg" → "eggway"  "inbox" → "inboxway"  "eight" → "eightway" You could use the split command to enter more than one word. Indicating delimiters char[] delimiterChars = { ' ', ',', '.', ':', ';', '\t' }; string[] words = text.Split(delimiterChars);

User Tzlil
by
4.3k points

1 Answer

1 vote

Answer:

Hi there Sandrathompson5661!

Using Java as the language for implementation, the PigLatin translation program can simply be coded as shown below. The program uses the Scanner module in java utils package to prompt the user for input of word(s) and splits the input with any of the matching delimiters. The rule to swap the first character and append it to the end with an “ay” for a normal word, or the appending of “yay” for a vowel word, is then applied. The result is printed on the screen before the program exits.

Step-by-step explanation:

import java.util.Scanner;

public class PigLatin {

public static void main(String[] args) {

char[] delimiterChars = { ' ', ',', '.', ':', ';', '\t' };

String[] words_array;

Scanner words = new Scanner(System.in);

System.out.println("Please enter a word (for multiple words use any of the following delimiters {' ', ',', '.', ':', ';', '\t' }): ");

String input_words = words.nextLine();

words_array = input_words.split("[,\\s\\-:.;' '\\\t]");

System.out.println("PigLatin translation: ");

for (int i=0; i<words_array.length; i++) {

System.out.println(makePigLatin(words_array[i]));

}

}

public static String makePigLatin(String word) {

String vowels = "aeiou";

if (vowels.indexOf(Character.toLowerCase(word.charAt(0))) != -1) {

word = word + "yay";

}

else {

word = word.substring(1, word.length()) + word.charAt(0) + "ay";

}

return word;

}

}

User Sharathnatraj
by
5.2k points