Pig Latin Program in Java
Pig Latin Program in Java
Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps. The Pig Latin program in Java generates a Pig Latin word based on the input given by a user.
STEP 1: Take a word as input from the user.
STEP 2: Take the first vowel of word and place it at the beginning of the new word. If the vowels are absent in the input string, the Pig Latin encoding is impossible.
STEP 3: Take all the letters following the first vowel of the input word and place them behind the first letter of the new word.
STEP 4: Append all the letters coming before the first vowel of the input word to the new word.
STEP 5: Append ay to the new word. Now, the new word is the Pig Latin word.
Let’s understand the above steps through an example.
Suppose we take the word ‘goat’. The first occurrence of a vowel in the word ‘goat’ is the letter ‘o’. Now, we place the letter ‘o’ at the beginning of the new word. After that, append all the remaining letters (coming after vowel i.e. at) after the vowel. Thus, the new word becomes ‘oat’. Now, append all the letters coming before the first vowel i.e. g to the new word. The new word becomes ‘oatg’.
At last, append ‘ay’ to the new word. Thus, the updated new word is ‘oatgay’ that is a Pig Latin word for the word ‘goat’.
Let’s implement the above steps in a Java program.
Filename: PigLatinExample.java
// importing the Scanner class import java.util.Scanner; public class PigLatinExample { // Method of checking vowels public static Boolean isVowel(char c) { // handling the case in-sensitivity if(c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U') // if we reach here, we got a vowel return true; //if we reach here, we got a consonant return false; } // Method for encoding the input string public static String findPigLatin(String input) { // calculating length of the string int size = input.length(); String pigL = ""; // contains final answer int i; // loop for finding the first vowel in the string for(i = 0; i < size; i++) { if(isVowel(input.charAt(i))) { // got the first vowel, // getting out of the for-loop break; } } // vowels are not present in the input string. if( i == size) { // retuning an empty string return ""; } // if we reach here, at least one vowel is present in the input string // the first vowel and following letters should come // as it is in the encoded string pigL = input.substring(i); // Appending the letters appearing before // the first vowel in the input string pigL = pigL + input.substring(0, i - 0); // Final step, appeding the string "ay" pigL = pigL + "ay"; // returning the encoded Pig Latin string return pigL; } public static void main(String argvs[]) { String str = ""; // contains the encrypted Pig Latin string // Creating an object of the Scanner class Scanner scnr = new Scanner(System.in); System.out.println("Enter a string "); // reading input given from the user str = scnr.nextLine(); // calling the method findPigLatin() and storing the outcome String ans = findPigLatin(str); // displaying the result if(ans.equals("")) { System.out.println("The Pig Latin encoded string of the input string " + str + " is not possible"); } else { System.out.println("The Pig Latin encoded string of the input string " + str + " is " + ans); } } }
Output 1:
Enter a string trIpptt The Pig Latin encoded string of the input string trIpptt is Ipptttray
Output 2:
Enter a string ptyrsdf The Pig Latin encoded string of the input string ptyrsdf is not possible
Explanation: The code written above is completely based on the steps written above. We have taken a string form the user and passed it as argument in the method findPigLatin(). The method findPigLatin() iterates over each character of the input string and invokes the Boolean method isVowel(). In each iteration, the method checks whether vowels are present in the input string or not. The iteration continues till the method isVowel() returns true or the condition part of the for-loop evaluates false. If the loop is terminated because of the break statement, this means the string has at least one vowel. Using the index of the first vowel, we do the Pig Latin encoding of the input string. If the loop is terminated because of the condition part of the loop evaluated false, we cannot do the Pig Latin encoding of the input word. It is because the input word contains no vowel.