-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPigLatinTranslator.java
93 lines (80 loc) · 2.08 KB
/
PigLatinTranslator.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package jr.eecs1022.piglatin;
import java.util.StringTokenizer;
public class PigLatinTranslator
{
private String english;
private String pig;
public static final String vowel = "aeiou";
public PigLatinTranslator()
{
this.setEnglish("");
}
public PigLatinTranslator(String text)
{
this.setEnglish(text);
}
public String getEnglish()
{
return this.english;
}
public void setEnglish(String text)
{
this.english = text.toLowerCase();
this.translate();
}
public String getPig()
{
return this.pig;
}
// Translate the state to PigLatin
public void translate()
{
StringTokenizer st = new StringTokenizer(this.english);
String result = "";
while (st.hasMoreTokens())
{
String word = st.nextToken();
String pig = this.translateWord(word);
if (result.length() == 0)
{
result = pig;
}
else
{
result = result + " " + pig;
}
}
this.pig = result;
}
// Translate the given word to PigLatin
// and return the translation
private String translateWord(String word)
{
// replace with correct code
int hasVowel = -1;
int i = 0;
while((hasVowel == -1) && (i < word.length())){
if (vowel.indexOf(word.charAt(i)) != -1){
hasVowel = i;
}
i++;
}
if (hasVowel == -1){
word += "ay";
}else{
if (hasVowel == 0){
word += "way";
}else{
String newWordBegin = word.substring(hasVowel, word.length());
String newWordEnd = word.substring(0, hasVowel);
word = newWordBegin + newWordEnd + "ay";
}
}
System.out.printf("/n");
String result = word;
return result;
}
public static void main(String[] args){
PigLatinTranslator plt = new PigLatinTranslator();
}
}