Question

/** * Returns the result of converting s to "Pig Latin". Convert a string s to...

/**

* Returns the result of converting s to "Pig Latin". Convert a string s to Pig Latin by using the following rules:

*

* (1) If s contains no vowels, do nothing to it.

*

* (2) Otherwise, if s starts with a vowel, append "way" to the end.

*

* (3) Otherwise, move everything up to (but not including) the first vowel to the end and add "ay".

*

* For example, "hello" converts to "ellohay", "small" converts to "allsmay", "eat" converts to "eatway", and "nth"

* converts to "nth".

*

* IMPLEMENTATION NOTE: This will require a three-way conditional that extracts pieces of Strings and recombines

* them into new Strings. For full credit (and an easier implementation), use the firstVowelIndex method provided

* above in your method's implementation. You will also find the methods s.substring() and s.charAt() (where s is a

* String), as well as the + operator that concatenates Strings, very useful.

*/

public static String toPigLatin (String s)

{

return "";

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Have in mind that you didn't include the firstVowelIndex method provided, which is required for full credit. However, I implemented one assuming it would be static, however, if the one the problem provides is static, you can remove my implementation from the code, just the -public static Integer firstVowelIndex (String s)- block but, if it is not static, then you would have to change inside toPigLatin method, where it says firstVowelIndex(s) to s.firstVowelIndex()

public class Main {

    public static void main(String[] args){
        String test="nth";
        toPigLatin(test);
    }

    public static String toPigLatin (String s)
    {
        String back="";
        String ending="";
        String part="";
        Boolean flag=false;
        for (int i=0;i<s.length();i++){
            char c=s.charAt(i);
            if (c=='A' || c=='E' || c=='I' || c=='O' || c=='U' || c=='a' || c=='e' || c=='i' || c=='o' || c=='u'){
                flag=true;
                back=s;
                break;
            }
        }

        if (flag){
            if (firstVowelIndex(s)==0){
                ending="way";
                back=s+ending;
            }
            else
            {
                ending="ay";
                for (int i=0; i<firstVowelIndex(s);i++) {
                    part=part+s.charAt(i);
                }
                back=s.substring(firstVowelIndex(s))+part+ending;
            }
        }
        else
        {
            back=s;
        }
        System.out.println(back);
        return back;
    }

    public static Integer firstVowelIndex (String s)
    {   int index=-1;
        for (int i=0; i < s.length(); i++){
            char c= s.charAt(i);
            if (c=='A' || c=='E' || c=='I' || c=='O' || c=='U' || c=='a' || c=='e' || c=='i' || c=='o' || c=='u'){
                index=i;
                break;
            }
        }
        return index;
    }
}
Add a comment
Know the answer?
Add Answer to:
/** * Returns the result of converting s to "Pig Latin". Convert a string s to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Pig Latin program using Linked Lists C++

    Write a program that prompts the user to input a string and then outputs the string in the pig Latin form. The rules for converting a string into pig Latin form are asfollows:a. If the string begins with a vowel, add the string "-way" at the end of the string. for example, the pig Latin form of the string "eye" is "eye-way".b. If the string does not begin with a vowel, first ass "-" at the end of the string....

  • There is a children's "secret language" called "Pig Latin" which supposedly allows conversation the uninformed (such...

    There is a children's "secret language" called "Pig Latin" which supposedly allows conversation the uninformed (such as parents) cannot understand. The rules are: If a word begins with a vowel (aeiou) then append "way" to the word. For example, "Aggie" becomes "Aggieway". Otherwise, remove the consecutive non-vowels from the beginning of the word, append them to the end of the word, and append "ay" to the word. For example, "whoop" becomes "oopwhay". Write a program named hw5pr1.cpp which reads words...

  • In C Sixth: Pig Latin (10 Points) For this part of the assignment, you will need...

    In C Sixth: Pig Latin (10 Points) For this part of the assignment, you will need to write a program that reads an input string representing a sentence, and convert it into pig latin. We'll be using two simple rules of pig latin: 1. If the word begins with a consonant then take all the letters up until the first vowel and put them at the end and then add "ay" at the end. 2. If the word begins with...

  • Design and code a SWING GUI to translate text entered in English into Pig Latin. You...

    Design and code a SWING GUI to translate text entered in English into Pig Latin. You can assume that the sentence contains no punctuation. The rules for Pig Latin are as follows: For words that begin with consonants, move the leading consonant to the end of the word and add “ay”. Thus, “ball” becomes “allbay”; “button” becomes “uttonbay”; and so forth. For words that begin with vowels, add “way’ to the end of the word. Thus, “all” becomes “allway”; “one”...

  • Correct the syntax errors to allow the code to pass all of the provided doctests. You...

    Correct the syntax errors to allow the code to pass all of the provided doctests. You may have heard of "Pig Latin," a set of rules for modifying regular English to render it unintelligible to those who do not know the rules. This problem asks you to fix a function that will convert an English word to Pig Latin The rules for converting a word to Pig Latin are as follows: 1. If the word starts with a vowel, add...

  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT