This repository has been archived by the owner on Sep 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPigLatin.java
54 lines (48 loc) · 1.56 KB
/
PigLatin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.ArrayList;
import java.util.Scanner;
public class PigLatin {
public String line;
private boolean isVowel(String str) {
if ((str.substring(0, 1) == "a") || (str.substring(0, 1) == "e") || (str.substring(0, 1) == "i")
|| (str.substring(0, 1) == "o") || (str.substring(0, 1) == "u")) {
return true;
} else {
return false;
}
}
public String toPig(String str) {
if (isVowel(str)) {
return str + "yay";
} else {
return str.substring(1) + str.substring(0, 1) + "ay";
}
}
private ArrayList<String> getLineWords() {
ArrayList<String> words = new ArrayList<String>();
String newLine = line;
int index = newLine.indexOf(" ");
while (index != -1) {
String word = newLine.substring(0, index);
words.add(word);
newLine = newLine.substring(index + 1);
}
return words;
}
public void main() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a sentence to translate to Pig Latin: ");
String line = keyboard.nextLine();
keyboard.close();
System.out.println("Before: " + line);
String after = "";
ArrayList<String> words = getLineWords();
for (String word : words) {
after += " " + toPig(word);
}
System.out.println("After: " + after);
}
public static void main(String[] args) {
PigLatin pig = new PigLatin();
pig.main();
}
}