-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoGoatLatin.java
More file actions
40 lines (35 loc) · 1.25 KB
/
toGoatLatin.java
File metadata and controls
40 lines (35 loc) · 1.25 KB
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
import java.util.*;
public class toGoatLatin{
public static final Character[] vowelsList = new Character[]{'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
public static final HashSet<Character> vowels = new HashSet<Character>(Arrays.asList(vowelsList));
public static void main(String args[]){
String s="The quick brown fox jumped over the lazy dog";
String result=to_goatlatin(s);
System.out.println(result);
}
public static String to_goatlatin(String S) {
if(S == null || S.length() == 0) {
return "";
}
StringBuilder answer = new StringBuilder();
int wordIndex = 1;
for (String word : S.split(" ")) {
if (wordIndex != 1) {
answer.append(" ");
}
char firstCharacter = word.charAt(0);
if (vowels.contains(firstCharacter)) {
answer.append(word);
} else {
answer.append(word.substring(1));
answer.append(firstCharacter);
}
answer.append("ma");
for (int i = 0; i < wordIndex; i++) {
answer.append("a");
}
wordIndex++;
}
return answer.toString();
}
}