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 translates an English word into a Pig Latin word. Input ONE, and...
Write a program that translates an English word into a Pig Latin word. Input ONE, and ONLY one, word from the user, and then output its translation to Pig Latin. The rules for converting a word to Pig Latin follow. The general form for converting a word is to remove the first letter. Then append to the end of the word a hyphen, followed by the first letter that was removed, followed by "ay". For example, "robot" becomes "obot-ray" and...
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...
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)....
IN PYTHON 1) Pig Latin Write a function called igpay(word) that takes in a string word...
IN PYTHON 1) Pig Latin Write a function called igpay(word) that takes in a string word representing a word in English, and returns the word translated into Pig Latin. Pig Latin is a “language” in which English words are translated according to the following rules: For any word that begins with one or more consonants: move the consonants to the end of the word and append the string ‘ay’. For all other words, append the string ‘way’ to the end....
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...
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...
for eclipse java Overview Write a program that translates an English phrase into a Pig Latin...
for eclipse java Overview Write a program that translates an English phrase into a Pig Latin phrase. Input a phrase from the user and translate it to pig latin. You can assume the phrase will be on a single line. Use at least the REQUIRED METHODS listed below, with the exact same SPELLING for method names and parameters. Input Specification The input will be an English phrase, with NO terminal punctuation. Read the phrase as a SINGLE STRING using the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT