Skip to content

Latest commit

 

History

History
executable file
·
22 lines (14 loc) · 1.61 KB

f7c3.md

File metadata and controls

executable file
·
22 lines (14 loc) · 1.61 KB

Back to questions

f7c3: Pig Latin

An English word is translated into Pig Latin as follows: if the word begins with a consonant, this is moved to the end of the word and "ay" is appended to the word. Otherwise the word begins with a vowel, in which case "way" is simply added to the word. Letters are re-capitalized appropriately to stick to standard English language rules, and punctuation, numbers and whitespace are left intact. For instance, the following sentence:

How are you on January 1st? I am fine, thanks.

becomes:

Owhay areway ouyay onway Anuaryjay 1st? Iway amway inefay, hankstay.

This is actually a slight simplification of the true rules of Pig Latin: in practice, "consonant clusters", not just the leading consonant, should be moved to the end of a word, so that, e.g., "question" becomes "estionquay". We will ignore this complexity, so that "question" is translated to "uestionqay".

Write a Java program that reads text from standard input and translates it into Pig Latin. Reading from standard input can be performed as in question 2d33. Translating a single word into Pig Latin is quite easy. The challenge in this question is in how to decompose lines of input into individual words.

Hints: The String class provides a substring method which you will find useful for this question. To correctly preserve capitalisation and punctuation in your solution, and to handle numbers, you may find the static methods isUpperCase, toLowerCase and isDigit from the Character class useful. Refer to the Java documentation to learn more about these methods.