Question

In: Computer Science

In Java.This program will translate a word into pig-latin. Pig-latin is a language game in which...

In Java.This program will translate a word into pig-latin. Pig-latin is a language game in which words in English are altered, usually by removing letters from the beginning of a word and arranging them into a suffix. The rules we will use for the pig-latin in this program are as follows: If a word starts with a consonant, split the word at the first instance of a vowel, moving the beginning consonants to the end of the word, following a '-'. Then add 'ay' at the end of the word. Vowels are: a, e, i, o, u (Notice: We are not considering y a vowel for this program) If a word starts with a vowel add the word "yay" at the end of the word following a '-'. Examples: hello translates to ello-hay flare translates to are-flay it translates to it-yay A more detailed explanation of the requirements for each method will be in the method header comments - please follow these closely. Suggested order of completion: isVowel() then pigLatin().

Solutions

Expert Solution

Sol:

Here is the java code for above problem with proper comments just as mentioned in the question. The program uses two functions- isVowel() and pigLatin() to complete the task.

Code:

//java program for pig latin
class Main { 
//isVowel function returns 1 if a character passed is vowel and returns 0 if it's not a vowel
static boolean isVowel(char c) { 
        return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || 
                        c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); 
} 
//defining pigLatin function which will return pigLatin based on the word
//if the word starts with a vowel the -yay will be added at the end
//if the word starts with a consonant then it'll be checked for the first occurrence of vowel 
//and returns the word like (flare- are-flay)
//if the word doesn't have a vowel then piglatin is not possible
static String pigLatin(String s) { 

        // the index of the first vowel is stored. 
        int len = s.length(); 
        int index = -1; 
        if (isVowel(s.charAt(0))){
            return s+ "-" + "yay";
        }
        for (int i = 0; i < len; i++) 
        { 
                if (isVowel(s.charAt(i))) { 
                index = i; 
                break; 
        } 
        } 

        // Pig Latin is possible only if vowels 
        // is present 
        if (index == -1) 
                return "-1"; 

        // Take all characters after index (including 
        // index). Append all characters which are before 
        // index. Finally append "ay" 
        return s.substring(index) + "-" +
                s.substring(0, index) + "ay"; 
} 

// Main method
public static void main(String[] args) { 
        String str = pigLatin("flare"); 
        if (str == "-1") 
                System.out.print("No vowels found." + 
                                                "Pig Latin not possible"); 
        
        else
                System.out.print(str); 
} 
} 

Sample Output:


Related Solutions

Write a program that converts an English phrase into pseudo-Pig Latin phrase (that is Pig Latin...
Write a program that converts an English phrase into pseudo-Pig Latin phrase (that is Pig Latin that doesn't allow follow all the Pig Latin Syntax rules.) Use predefined methods of the Array and String classes to do the work. For simplicity in your conversion, place the first letter as the last character in the word and prefix the characters "ay" onto the end. For example, the word "example" would become "xampleay" and "method" would become "ethodmay." Allow the user to...
How do I write a COBOL program that translates a word entered into pig Latin?
How do I write a COBOL program that translates a word entered into pig Latin?
C++ Project 6-2: Pig Latin Translator Create a program that translates English to Pig Latin. Console...
C++ Project 6-2: Pig Latin Translator Create a program that translates English to Pig Latin. Console Pig Latin Translator This program translates a sentence and removes all punctuation from it. Enter a sentence: 'Tis but a scratch. Translation:      Istay utbay away atchscray Specifications Convert the English to lowercase before translating. Remove all punctuation characters before translating. Assume that words are separated from each other by a single space. If the word starts with a vowel, just add way to the...
question: Pig LatinPig latin, is a mock language which encrypts english sentences using the following rules:...
question: Pig LatinPig latin, is a mock language which encrypts english sentences using the following rules: For every word in a sentence, the firstletter of the word is shifted to the end of the word and the suffix “-ay” is added at the end of the word. For example, “hey pat”becomes “eyhay atpay”.Implement thepiglatin(str)function so it translates sentences to pig latin. If you have time remaining, try implementing afunction to ”decrypt” piglatin, i.e. given a sentence in piglatin, return the...
You can turn a word into pig-Latin using the following two rules: If the word starts...
You can turn a word into pig-Latin using the following two rules: If the word starts with a consonant, move that letter to the end and append 'ay'. For example, 'happy' becomes 'appyhay' and 'pencil' becomes 'encilpay'. If the word starts with a vowel, simply append 'way' to the end of the word. For example, 'enter' becomes 'enterway' and 'other' becomes 'otherway'. For our purposes, there are 5 vowels: a, e, i, o, u (we consider y as a consonant)....
Pig Latin is a language constructed by transforming English words. The following rules are used to...
Pig Latin is a language constructed by transforming English words. The following rules are used to translate English into Pig Latin: *If the word begins with a consonant, then all consonants at the beginning of the word, up to the first vowel are removed then added to the end of the word, followed by “ay”. For example, “computer” becomes “omputercay” and “think” becomes “inkthay”. *If the word begins with a vowel, then “way” is added to the end of the...
Create a program that translates English into Pig Latin. The function will take the first letter...
Create a program that translates English into Pig Latin. The function will take the first letter of each word in the sentence only if it’s a not a vowel, and place it at the end of the word followed by “ay”. Your program must be case ​ins​ ensitive. You must write the functiontranslate() ​that takes in a single English word and ​returns​ its Pig Latin translation. Remembertranslatemusttake​onlyonewordasinput.​ ############################################################# # translate() takes a single english word translates it to #pig latin...
PYTHON GAME OF PIG The game of Pig is a simple two player dice game in...
PYTHON GAME OF PIG The game of Pig is a simple two player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn a player rolls a six-sided die. After each roll: a) If the player rolls a 1 then the player gets no new points and it becomes the other player’s turn. b) If the player rolls 2-6 then they can either roll again or hold. If the player...
How would you go about writing a MATLAB program to convert a phrase in Pig Latin...
How would you go about writing a MATLAB program to convert a phrase in Pig Latin to English. I have already written a script that converts phrases from English into Pig Latin; however, I am not sure how to reverse the process. Can anyone who know's MATLAB please help me out, thank you? P.S. I know that this is an unambigious task so it doesn't have to work completely well. With the most minor assumptions made, if it could covert...
The game of Pig is a simple two-player dice game in which the first player to...
The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. How to play: Players take turns rolling one six-sided dice and following these rules: If the player rolls 2 through 6, then he/she can either a. “Roll Again” or b. “Hold” At this point, the sum of all rolls is added to the player’s score, and it becomes the other player’s turn. If the player rolls 1 before...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT