logo CodeStepByStep logo

pigLatin

Language/Type: Java file input
Related Links:
Author: Nick Troccoli (on 2017/04/25)

Write a method named pigLatin that accepts as a parameter a Scanner representing an input file. Your method should, preserving line breaks, print out the input file's text in a simplified version of Pig Latin, a silly English variant where the first letter of each word is moved to the end. The rules for translating a word to Pig Latin are as follows:

  • If the word starts with a vowel (aeiou), simply append "yay". For example, "elephant" becomes "elephantyay".
  • If the word starts with a consonant, move the consonant to the end, and append "ay". For example, "welcome" becomes "elcomeway".

As an overall example, if the input file lincoln.txt contains the following text:

four score and
seven years ago our
fathers brought forth on this continent a new nation

We might call your method as follows:

Scanner input = new Scanner(new File("lincoln.txt"));
pigLatin(input);

The above call should produce the following output:

ourfay coresay andyay
evensay earsyay agoyay ouryay
athersfay roughtbay orthfay onyay histay ontinentcay ayay ewnay ationnay
Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.