In: Computer Science
Java
If the word contains the letter "y", there are some special rules that must be applied:
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.
Program Specification
Input
Our program should accept input in one of two ways:
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!
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: