Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8970387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:43:17+00:00 2026-06-15T17:43:17+00:00

my assignment here was to create a pig latin translator using recursion, that could

  • 0

my assignment here was to create a pig latin translator using recursion, that could translate sentences. The rules were as follows:

  1. If there are no vowels in englishWord, then pigLatinWord is just englishWord + “ay”. (There are ten vowels: ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’, and their uppercase counterparts. ‘y’ is not considered to be a vowel for the purposes of this assignment, i.e. my becomes myay, why becomes whyay, etc.)
  2. Else, if englishWord begins with a vowel, then pigLatinWord is just englishWord + “yay”.
  3. Otherwise (if englishWord has a vowel in it and yet doesn’t start with a vowel), then pigLatinWord is end + start + “ay”, where end and start are defined as follows:
    –

Let start be all of englishWord up to (but not including) its first vowel.
Let end be all of englishWord from its first vowel on.
But, if englishWord is capitalized, then capitalize end and “uncapitalize” start.

Here’s my code so far (sorry about the weird formatting, my comments got messed up):

    /*Recursively Translate a String (without punctuation or numerical characters) to Pig Latin*/


//prep the string for translation and submit it to be translated
public static String translate(String finished) {
    finished.trim();        //Trim the String of whitespace at the front and end
    finished += " ";        //Because of the recursive method I use, the string must have a
                            //space at the end
    finished = translateSentence(finished); //recursively translate the string
    finished.trim();    //trim the whitespace added earlier
    return finished;    //Return the string
}


//recursively submits each word in the string to the translator, then 
//returns the translated sentence
private static String translateSentence(String finished) {

if (finished.length() == 0) {   //the base condition is met when each word in the string
   return finished; //has been sent to the translator (string is empty)
    }
    else {
    return (translateWord(finished.substring(0, finished.indexOf(' ') )) + " "
        + translateSentence(finished.substring(finished.indexOf(' ') + 1)));        
    }
}
/*If the base condition is not met, the method returns the first word of the string
 * (translated) and a space, then submits the rest of the 
 * string back to the method.  The first word is defined as the beginning 
 * of the string up until the first space. The rest of the string
 * starts one character after the space.   */



//Checks the submitted word for vowels and vowel placement, and translates accordingly

    private static String translateWord(String stringA) {
    if (stringA.indexOf('a') == -1
    && stringA.indexOf('e') == -1           //Checks for presence of any vowels
    && stringA.indexOf('i') == -1       //if no vowels are found
    && stringA.indexOf('o') == -1       //the word + ay is returned
    && stringA.indexOf('u') == -1
    && stringA.indexOf('A') == -1
    && stringA.indexOf('E') == -1
    && stringA.indexOf('I') == -1
    && stringA.indexOf('O') == -1
    && stringA.indexOf('U') == -1) {
        return stringA + "ay";
    }       
    if (stringA.charAt(0) == 'a'
    ||  stringA.charAt(0) == 'e' //checks if there is a vowel at the start
    ||  stringA.charAt(0) == 'i'//of the string. if there is a vowel
    ||  stringA.charAt(0) == 'o'    //it returns the word + yay
    ||  stringA.charAt(0) == 'u'
    ||  stringA.charAt(0) == 'A'
    ||  stringA.charAt(0) == 'E'
    ||  stringA.charAt(0) == 'I'
    ||  stringA.charAt(0) == 'O'
    ||  stringA.charAt(0) == 'U') {
        return stringA + "yay";
    }       
/* if the word has a vowel that isn't at the start, the part of the string


* before the first vowel is moved to the end of the vowel, and "ay" is added.


* However, if the first character in the word is capitalized, the first vowel becomes


* uppercase and the former first character in the word becomes lowercase */
    else {
        if (Character.isUpperCase(stringA.charAt(0))) {
            return Character.toUpperCase(stringA.charAt(firstVowel(stringA, 0))) 
            + stringA.substring(firstVowel(stringA, 0) + 1, stringA.length())
            + Character.toLowerCase(stringA.charAt(0))
            + stringA.substring(1, firstVowel(stringA, 0)) + "ay";
        }
        else {
        return stringA.substring(firstVowel(stringA, 0), stringA.length()) 
        + stringA.substring(0, firstVowel(stringA, 0)) + "ay";
        }
    }
}



//Recursively determines the index number of the first vowel in a given word
//0 must always be submitted as int x
public static int firstVowel(String stringA, int x) {
    if (x > stringA.length() - 1) {     //if the index number becomes greater than the length
        return -1;      //of the string, -1 (no vowels) is returned
    }
    if (stringA.charAt(x) == 'a' 
        || stringA.charAt(x) == 'e'     //the base condition is met when the character
        || stringA.charAt(x) == 'i'     //at the current index number is a vowel
        || stringA.charAt(x) == 'o'     //and the index number is returned
        || stringA.charAt(x) == 'u' 
        || stringA.charAt(x) == 'A' 
        || stringA.charAt(x) == 'E' 
        || stringA.charAt(x) == 'I' 
        || stringA.charAt(x) == 'O' 
        || stringA.charAt(x) == 'U') {
        return x;
    }
    else {
        return firstVowel(stringA, x + 1);  //otherwise, the string and the index number
    }                                       // + 1 are submitted back to the method
}

This gives me the desired output (“Why hello there” becomes “Whyay ellohay erethay”)
but right now it can’t handle punctuation. Basically what I’m looking for are any tips or help getting my code to handle punctuation, or any ways to improve my code (still using recursion) in general.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:43:17+00:00Added an answer on June 15, 2026 at 5:43 pm

    @Cedric give a good suggestion to make use of Regex in performing the processing.

    Here I give some other directions.

    First I believe you can have better reuse of your firstVowel method.

    Your translateWord can be greatly simplified as (I skip your upper-case handling to make it easier to understand):

    private static String translateWord(String s) {
        int firstVowelIndex = firstVowel(s);
        if (firstVowel < 0) { // no Vowel
            return s + "ay";
        } else if (firstVowel == 0) {  // start with Vowel
            return s + "yay";
        } else {
            return s.substring(firstVowelIndex) + s.substring(0, firstVowelIndex) + "ay";
        }
    }
    

    There are something to simplify in your firstVowel() too.

    First the comparison of Vowel can be simplified. One way is to use regex. Another way is by using a Set:

    assume you have something like this:

    static Set<Character> vowels = new HashSet<Character>(
            Arrays.asList('a','e','i','o','u','A','E','I','O','U'));
    

    Then your vowel checking can be changed from a long if statement to something like:

    if (vowels.contains(c)) {...}
    

    Second, you may be intentionally writing firstVowel() in a recursive manner for practice. However, it seems to me that the method is much easier to read if it is wrote in form of a loop:

    private static int firstVowel(String s) {
        int index = -1;
        for (int i = 0; i < s.length; ++i ) {
            if (isVowel(s.charAt(i)) {
                index = i;
                break;
            }
        }
        return index;
    }
    

    I believe simply these two changes can almost half your code, in a more readable manner.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Java tutorials here mention that &= is an assignment operator but doesn't seem
So, here's the story. I'm trying to create a recursive descent parser that tokenizes
I've been working on Java homework. Here are my instructions: Interface Programming Assignment Create
This is an assignment question from school: Create a recursive method called toBinary that
Please help with my assignment. Here is the question: Create a separate test driver
Ive got an assignment to create an image gallery using flickr - this has
I have an assignment to create a print view using HTML & CSS. This
I have an assignment to create a program that prompts user input of an
I'm working on a homework assignment that asks me to create a calculator that
Ok, here is my assignment at school. Create a console application named CreateVehicle. Develop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.