Quick actions

cmd+k|ctrl+k

Navigation

Languages

Pig Latin

Snippet info

Language

Java

Visibility

public

Author

hospino11

Created

2026-02-03T03:15:47.840719Z

Updated

2026-02-03T03:15:47.840719Z

// 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("apple")); // should print out "apple-way"
    System.out.println(pigLatinize("aaaapple")); // should print out "aaaaple-way"
    System.out.println(pigLatinize("hello world")); // should print out "apple-way"

  }

  // Implement this method:
  public static String pigLatinize(String phrase) {
    int size = 0;

    String[] words = phrase.split(" ");
    List<String> output = new LinkedList<>();
    String newWord = null;
    // Range of ASCII vowels
    for (String word : words) {
      size = word.length();
      for (int i = 0; i < size; i++) {
        char currentChar = word.charAt(i);
  
        // Check if the char is vowel
        if (currentChar == 'a' || currentChar == 'e' || currentChar == 'i' || currentChar == 'o' || currentChar == 'u') {
          if (i == 0) {
             newWord = word + "-" + LATIN_WITH_VOWEL_SUFFIX;
             output.add(newWord);
             
          } else {
            newWord = word.substring(0, i);
            String remainingChars = word.substring(i, word.length());
            String latinizedWord = remainingChars + "-" + newWord + LATIN_INDEX;
            output.add(latinizedWord);
          }
          break;
        }
      }
    }

    String latinizedWord = output.stream().collect(Collectors.joining(" "));

    return latinizedWord;
  }
}
INFO