Quick actions

cmd+k|ctrl+k

Navigation

Languages

Pig Latin (v2)

Snippet info

Language

Java

Visibility

public

Author

hospino11

Created

2026-02-03T03:16:23.048316Z

Updated

2026-02-03T04:39:49.341355Z

// To run the code at any time, please hit the run button located in the top left corner.

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

class Main {

  private static final String LATIN_INDEX = "ay";
  private static final String LATIN_WITH_VOWEL_SUFFIX = "way";
  
  /**
   * Intro
   * To solve this challenge, feel free to use any and all resources available to you. Once you start the exercise, you'll have two hours to complete and submit your solution.
   * Challenge - Pig Latin
   * Pig Latin is a farcical "language" used to entertain children, but also to teach them some valuable language concepts along the way. Translating English to Pig Latin is simple:
   * 1) Take the first consonant (or cluster of consonants) of a word, move it to the end of the word, and add a suffix of "ay" 2) If a word begins with a vowel, just add "way" at the end 3) For the sake of readability, separate the Pig Latin-ized parts of the word with a dash -
   * Your challenge is to implement the method pigLatinize that takes a string phrase and translates it to Pig Latin. You're free to add additional classes, variables, and methods if you would like.
   * The input phrase could be as short as a single word, or as long as multiple sentences or paragraphs. Whitespace, capitalization, and punctuation should be honored and maintained in your final answer.
   * Examples
   * 1) "pig" => "ig-pay" 2) "pig latin" => "ig-pay atin-lay" 3) "Pig Latin" => "ig-Pay atin-Lay"
   */

  public static void main(String[] args) {
    System.out.println(pigLatinize("pig")); // should print out "ig-pay"
    System.out.println(pigLatinize("pig latin")); // should print out "ig-pay atin-lay"
    System.out.println(pigLatinize("Pig Latin")); // should print out "ig-Pay atin-Lay"
    System.out.println(pigLatinize("apple")); // should print out "apple-way"
    System.out.println(pigLatinize("hello world")); // should print out "apple-way"
    System.out.println(pigLatinize("pretty snake")); // should print out "retty-pray ake-snay"

  }

  // Implement this method:
  public static String pigLatinize(String phrase) {
    int size = phrase.length();
    StringBuilder output = new StringBuilder();
    String newWord = null;
    // Range of ASCII vowels
    
    // 
    int previousSpaceIndex = 0;
    int nextSpaceIndex = 0;
    
    for (int i = 0; i < size; i++) {
        char currentChar = phrase.charAt(i);
  
        // Check if the char is vowel
        if (currentChar == 'a' || currentChar == 'e' || currentChar == 'i' || 
            currentChar == 'o' || currentChar == 'u' || currentChar == 'A' || 
            currentChar == 'E' || currentChar == 'I' || currentChar == 'O' || 
            currentChar == 'U') {
                
            // We set the initial or left index. It is the beginning of the word
            // we set it as 0. Otherwise, we use the last space index to move
            // the previous space index one step ahead
            previousSpaceIndex = nextSpaceIndex > 0 ? nextSpaceIndex + 1 : 0;
            // We check the next space to slice the phrase
            nextSpaceIndex = phrase.indexOf(' ', previousSpaceIndex + 1);
            
            // If there is no more space characters, the end of the word will be the 
            // end of the phrase
            if (nextSpaceIndex == -1) {
                nextSpaceIndex = phrase.length();
            }
            
            String word = phrase.substring(previousSpaceIndex, nextSpaceIndex);
            if (i == 0) {
                newWord = word + "-" + LATIN_WITH_VOWEL_SUFFIX;
                output.append(newWord);
            } else {
                newWord = word.substring(0, i - previousSpaceIndex);
                String remainingChars = word.substring(i - previousSpaceIndex, word.length());
                String latinizedWord = remainingChars + "-" + newWord + LATIN_INDEX;
                output.append(latinizedWord);
            }
            if (nextSpaceIndex == phrase.length()) {
                break;
            } else {
                i = nextSpaceIndex;
                output.append(" ");
            }
        }
    }
    

    return output.toString();
  }
}
INFO