|
|
@@ -1,10 +1,37 @@
|
|
1
|
1
|
package rocks.zipcode.io.quiz3.fundamentals;
|
|
2
|
2
|
|
|
|
3
|
+import java.util.Arrays;
|
|
|
4
|
+
|
|
3
|
5
|
/**
|
|
4
|
6
|
* @author leon on 09/12/2018.
|
|
5
|
7
|
*/
|
|
6
|
8
|
public class PigLatinGenerator {
|
|
|
9
|
+ private static final char[] vowels = {'a', 'e', 'i', 'o', 'u'};
|
|
7
|
10
|
public String translate(String str) {
|
|
8
|
|
- return null;
|
|
|
11
|
+ String fin = "";
|
|
|
12
|
+ String[] wordArr = str.split(" ");
|
|
|
13
|
+ for(String word : wordArr){
|
|
|
14
|
+ fin = fin.concat(translateWord(word));
|
|
|
15
|
+ }
|
|
|
16
|
+ return fin;
|
|
|
17
|
+ }
|
|
|
18
|
+ public String translateWord(String word){
|
|
|
19
|
+ int start = 0; // start index of word
|
|
|
20
|
+ int firstVowel = 0;
|
|
|
21
|
+ int end = word.length(); // end index of word
|
|
|
22
|
+ for(int i = 0; i < end; i++) { // loop over length of word
|
|
|
23
|
+ char c = Character.toLowerCase(word.charAt(i)); // char of word at i, lower cased
|
|
|
24
|
+ if(Arrays.asList(vowels).contains(c)) { // convert vowels to a list so we can use List.contains() convenience method.
|
|
|
25
|
+ firstVowel = i;
|
|
|
26
|
+ break;
|
|
|
27
|
+ }
|
|
|
28
|
+ }
|
|
|
29
|
+ if(start != firstVowel) { // if start is not equal to firstVowel, we caught a vowel.
|
|
|
30
|
+ String startString = word.substring(firstVowel, end);
|
|
|
31
|
+ String endString = word.substring(start, firstVowel) + "ay";
|
|
|
32
|
+ return startString+endString;
|
|
|
33
|
+ }
|
|
|
34
|
+ return word; //couldn't find a
|
|
|
35
|
+ }
|
|
9
|
36
|
}
|
|
10
|
|
-}
|
|
|
37
|
+
|