Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

or the purposes of this problem, the rules for translating an English word to Pi

ID: 3681438 • Letter: O

Question

or the purposes of this problem, the rules for translating an English word to Pig Latin are as follows: (i) Words that start with a vowel (a, e, i, o, u) simply hv "ay" ap- pended to the end of the word ii) Words that start with a consonant have all consonant letters up to the first vowel moved to the end of the word, and "ay is appended or example, the English word "art" is translated to "artway" whercas choo" is translated to "oolschay a) Fill in the blanks below to give a function translate that translates a single word into Pig Latin: translateString String translate cs pre""- I otherzise here way" ay" isConsonant: Char Bool isConsonant c not (elem (toLower c) "aeiou") (pre , rest) = Hint: The functions takewhile and dropwhile are useful here

Explanation / Answer

import java.io.*;

public class PigLatin

{

    public static void main(String args[])throws IOException

    {

        String word;

        String changed_word;

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter a word:");

        word = br.readLine();

        changed_word = Translate(word);

        System.out.println("The word in Pig Latin is :"+ changed_word);

    }

    public static String Translate(String abc)

    {

        int i = 0, j;

        String temp = "";

        String temp2 = "";

        if(abc.charAt(i) == 'A' || abc.charAt(i) == 'a' || abc.charAt(i) == 'E' || abc.charAt(i) == 'e' || abc.charAt(i) == 'I' || abc.charAt(i) == 'i' || abc.charAt(i) == 'O' || abc.charAt(i) == 'o' || abc.charAt(i) == 'U' || abc.charAt(i) == 'u')

        {

            temp = abc + "way";

        }

        else

        {

            if(abc.charAt(i) == 'Y' || abc.charAt(i) == 'y')

            {

                for(j = i+1; j < abc.length(); j++)

                {

                    temp = temp + abc.charAt(j);

                }

                temp = temp + abc.charAt(i);

                temp = temp + "ay";

            }

            else

            {

                //System.out.println("Hi");

                for(i = 0; i < abc.length(); i++)

                {

                    if(abc.charAt(i) != 'A' && abc.charAt(i) != 'a' && abc.charAt(i) != 'E' && abc.charAt(i) != 'e' && abc.charAt(i) != 'I' && abc.charAt(i) != 'i' && abc.charAt(i) != 'O' && abc.charAt(i) != 'o' && abc.charAt(i) != 'U' && abc.charAt(i) != 'u' && abc.charAt(i) != 'Y' && abc.charAt(i) != 'y')

                    {

                        temp2 = temp2 + abc.charAt(i);

                    }

                    else

                    {

                        while(i < abc.length())

                        {

                            temp = temp + abc.charAt(i);

                            i++;

                        }

                        temp = temp + temp2;

                        temp = temp + "ay";

                        break;

                    }

                }

            }

        }

        return temp;

    }

}

The program above runs brilliantly on all the inputs.