Pig Latin is a playful linguistic activity in which words are modified by changing the order of their letters and incorporating a suffix.
There are multiple scenarios for converting words into Pig Latin. The rules are different for each of the following scenarios:
The words start with a consonant.
The words start with a vowel.
The words start with a consonant cluster.
The words have no vowels.
Assuming the word starts with a consonant, the letters that appear before the first vowel will be moved to the end of the word. Once that is done, the suffix “ay” will be appended to the word. Here are some examples to illustrate this rule:
“Table” becomes “abletay”:
We first identify the word's first consonant sound, “t.”
Then we move all the letters before the first consonant sound to the end of the word: “able.”
Lastly, we add the suffix “ay” to the end of the word: “abletay.”
If a word starts with a vowel, the suffix “yay” is added to the end of the word. However, some may add alternative endings such as “way” or “hay.” Here are some examples of words that illustrate this rule:
“Elephant” becomes “elephantay”:
As we can see that the word begins with a vowel, no letters need to be moved to the end of the word.
We add the suffix “ay” to the end of the word: “elephantay.”
Words that start with more than one consonant are called consonant cluster words. This means that multiple consonants that are pronounced together form a consonant cluster. In such cases, the entire sound is moved to the end of the word and followed by the suffix “ay.” Here are some examples of words that demonstrate this rule:
“Spring” becomes “ingspray”:
First, we identify the consonant cluster sound at the beginning of the word: “spr.”
Then we move the entire consonant cluster to the end of the word: “ingspr.”
Lastly, we add the suffix “ay” to the end of the word: “ingspray.”
Creating a Pig Latin word is impossible if the input word or string contains no vowels. For example, the Pig Latin of the word “rhythm” is impossible.
The code for writing a Pig Latin translator in Java is shown below:
import java.util.ArrayList;import java.util.List;public class PigLatinTranslator {public static void main(String[] args) {List<String> words = new ArrayList<>();words.add("hello");words.add("world");words.add("pig");words.add("latin");for (String word : words) {if (word.matches("[aeiou].*")) {// Word starts with a vowelSystem.out.println(word + "way");} else {// Word starts with a consonantint firstVowelIndex = word.indexOf(word.replaceAll("[^aeiouy]", "").charAt(0));String pigLatin = word.substring(firstVowelIndex) + word.substring(0, firstVowelIndex) + "ay";System.out.println(pigLatin);}}}}
Lines 6–10: We create a new array list called words
and add some example words to translate.
Line 11: We use a for
loop to iterate through each word in the words
list and apply the Pig Latin translation rules.
Lines 12–14: We use a regular expression to check if the word starts with a vowel. If it does, we simply add "way"
to the end of the word, and output the translated word to the console using System.out.println()
.
Lines 15–18: We check if the word starts with a consonant, then we need to rearrange the letters of the word. To do this, we first find the index of the first vowel in the word by using a regular expression to remove all non-vowel characters and take the first character of the resulting string. We then use substring()
to extract the prefix and suffix of the word, rearrange the letters, and add "ay"
to the end.
Line 18: We output the translated word to the console using System.out.println()
.
Overall, this Pig Latin translator implementation in Java is relatively straightforward and demonstrates how we can use regular expressions and string manipulation to solve a simple problem.
Free Resources