Question

In: Computer Science

Java If the word begins with a single consonant followed by a vowel, move the consonant...

Java

  1. If the word begins with a single consonant followed by a vowel, move the consonant to the end of the word and add “ay” after.
    • hello → [h]ello → ello[h] → elloh[ay] → ellohay
    • word → [w]ork → ork[w] → orkw[ay] → orkway
  2. If the word begins with a consonant cluster (more than one consonant followed by a vowel), move the entire consonant cluster to the end of the word and add “ay” after.
    • start → [st]art → art[st] → artst[ay] → artstay
    • through → [thr]ough → ough[thr] → oughthr[ay] → oughthray
  3. If the word begins with a vowel, simply add “yay” to the end of the word.
    • on → [o]n → on[yay] → onyay
    • a → [a] → a[yay] → ayay
    • unique → [u]nique → unique[yay] → uniqueyay

If the word contains the letter "y", there are some special rules that must be applied:

  1. If the word begins with the letter “y” followed by a vowel, treat the “y” as a consonant.
    • yes → [y]es → es[y] → esy[ay] → esyay
    • yellow → [y]ellow → ellow[y] → ellowy[ay] → ellowyay
  2. If the word begins with a consonant or consonant cluster followed by the letter “y”, treat the “y” as a vowel.
    • thy → [th]y → y[th] → yth[ay] → ythay
    • rhythm → [rh]ythm → ythm[rh] → ythmrh[ay] → ythmrhay

In the rules above, the letters "a", "e", "i", "o", and “u” are considered vowels, and all other letters are considered consonants, except for “y” as described above.

Other Characters

Beyond translating words, there are a few other rules to be aware of.

  1. If the original word’s first letter is capitalized, the translated word should also have its first letter capitalized.
    • Hello → [H]ello → ello[h] → elloh[ay] → [e]llohay → Ellohay
    • I → [I] → I[yay] → Iyay
  2. If the original word ends in a punctuation, move the punctuation to the end of the translated word.
    • hello. → hello[.] → [h]ello → ello[h] → elloh[ay] → ellohay[.] → ellohay.

Program Specification

Input

Our program should accept input in one of two ways:

  1. If a command-line argument is provided, it should read input from the file given as that argument.
  2. If no command-line arguments are provided, the program should read input directly from the terminal.

Each run of the program may consist of multiple lines of input. The total number of lines will vary. Input will end with either an EOF (end of file) marker, or a blank line of input. Your program should stop accepting input when either of these situations occurs.

Each line of input will contain words to be converted to Pig Latin. You may assume that the lines will only contain letters a-z A-Z, spaces , and common punctuation marks that are placed at the end of words !.,?.

You may assume that words are separated by spaces, but they may have attached punctuation marks at the end.

Input 1

hello world

Output 1

ellohay orldway

Input 2

A good day to Kansas!

Output 2

Ayay oodgay ayday otay Ansaskay!

Solutions

Expert Solution

Code:

import java.io.*;

public class Main {

 // taking input
  private static BufferedReader buf = new BufferedReader(
      new InputStreamReader(System.in));

  // calling main function
  public static void main(String[] args) throws IOException {

    // Get a string
    System.out.print("Enter sentence: ");
    String english = getString();

    // Translate and print back out
    String translated = translate(english);
    System.out.println(translated);
  }

  // extracting words
  private static String translate(String s) {
    String latin = "";
    int i = 0;
    while (i<s.length()) {

      // Take care of punctuation and spaces
      while (i<s.length() && !isLetter(s.charAt(i))) {
        latin = latin + s.charAt(i);
        i++;
      }

      // If there aren't any words left, stop.
      if (i>=s.length()) break;

      // Otherwise we're at the beginning of a word.
      int begin = i;
      while (i<s.length() && isLetter(s.charAt(i))) {
        i++;
      }

      // Now we're at the end of a word, so translate it.
      int end = i;
      latin = latin + pigWord(s.substring(begin, end));
    }
    return latin;
  }

  // confirm if its a letter
  private static boolean isLetter(char c) {
    return ( (c >='A' && c <='Z') || (c >='a' && c <='z') );
  }

  private static boolean isUpper(char c) {
    return ( c >='A' && c <='Z' );
  }

  // working on each word to translate
  private static String pigWord(String word) {
    int split = firstVowel(word);
    if(split!=0){
      if(isUpper(word.charAt(0))){
        String w = word.substring(split);
        String wr = w.substring(0,1).toUpperCase();
        String ws = word.substring(0,split);
        return wr+w.substring(1,w.length())+ws.toLowerCase()+"ay";
      }else{
        return word.substring(split)+word.substring(0, split)+"ay";
      }
    }else{
      if(isUpper(word.charAt(0))){
        return word.substring(0,1).toUpperCase()+word.substring(1,word.length())+"yay";
      }else{
        return word+"yay";
      }
    }
  }

  // finding the vowel for splitting
  private static int firstVowel(String word) {

    word = word.toLowerCase();
    int flag=0;
    for (int i=0; i<word.length(); i++){
      if (word.charAt(i)=='a' || word.charAt(i)=='e' ||
          word.charAt(i)=='i' || word.charAt(i)=='o' ||
          word.charAt(i)=='u' || word.charAt(i)=='y')
        if(i==0 && word.charAt(0)=='y'){
          flag=1;
        }else{
          return i;
        }
      if(flag==1){
        continue;
      }
    }
    return 0;
  }

  // read input
  private static String getString() throws IOException {
    return buf.readLine();
  }
}

Output:


Related Solutions

Write a Java program that directs the user to enter a single word (of at least...
Write a Java program that directs the user to enter a single word (of at least four characters in length) as input and then prints the word out in reverse in the pattern shown below (please note that the spaces added between each letter are important and should be part of the program you write): <Sample Output Enter a word (at least four characters in length): cat Word must be at least four characters in length, please try again. Enter...
in java Read in a word and display the requested characters from that word in the...
in java Read in a word and display the requested characters from that word in the requested format. Write a Java program that ● prompts the user for a word and reads it, ● converts all characters of the input word to uppercase and display the word with a double quotation mark ( " ) at the start and end of the word, ● displays the word with all characters whose index is odd in lower case and for the...
Write a program in JAVA to create the move set of a Pokémon, and save that...
Write a program in JAVA to create the move set of a Pokémon, and save that move set to a file. This program should do the following: Ask for the pokemon’s name. Ask for the name, min damage, and max damage of 4 different moves. Write the move set data into a file with the pokemon’s name as the filename. The format of the output file is up to you, but keep it as simple as possible
n a 800 word essay describe the path followed by an amino acid from absorption to...
n a 800 word essay describe the path followed by an amino acid from absorption to delivery to a cell. Compare this path with the one followed by a large fatty acid. Support your essay with scholarly resources.
You are given a task to parse through financial information and find the word “money” followed...
You are given a task to parse through financial information and find the word “money” followed by “bad” 2 or more times or “money” followed by “good” once. Create a regex that can search achieve this. You can ignore spaces. Legal words in the language could be “moneybadbadbad” or “moneygood”
Move all zeros in an arraylist to the end of the list using a temp(java). if...
Move all zeros in an arraylist to the end of the list using a temp(java). if it is an array the code would look like the code below, change it to fit an arraylist int count = 0;     int temp;     for (int i = 0; i < n; i++) {     if ((arr[i] != 0)) {         temp = arr[count];         arr[count] = arr[i];         arr[i] = temp;         count = count + 1;     }     } }
Write a 350-word memo to Client X recommending the move to IFRS or the stay with...
Write a 350-word memo to Client X recommending the move to IFRS or the stay with GAAP, and why.
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The program asks the user questions and then creates a paragraph using the user’s answers. 2.The program must perform the following: a.Uses a Scanner object to ask the user: (The program asks for no other information) i.Full Name (First and Last name only) - stores this Full Name in one String object variable. ii.Age – must be read in as an int. iii.Profession or expected...
Move the file “nfile2” into the “unix” directory. Copy the file “nfile1” into the “java” directory....
Move the file “nfile2” into the “unix” directory. Copy the file “nfile1” into the “java” directory. Show how you would create the files “test1”, “test2”, & “test3” in the “test” directory to ensure that they have the same file creation date/time. From the ~ folder, use the * and then the [ ] wildcard procedures to list all the files in the “test” directory. From the directory structure above, give an example of a relative path & an absolute path....
THIS IS FOR JAVA Given an oversize array of size words and a word to remove,...
THIS IS FOR JAVA Given an oversize array of size words and a word to remove, write a method that returns the array with each occurrence of the given word removed. Shift the remaining words in the nonempty part of the array to the left so that each occurrence of the given word is overwritten. (Leave the words in the empty part of the array unchanged.) Hint: To understand the test cases, note that the size (but not the capacity)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT