2
public String toGoatLatin(String sentence) {3
StringBuffer sb = new StringBuffer();4
StringBuffer temp = new StringBuffer("a"); // temporary stringbuffer6
for (String str : sentence.split(" ")) {7
if (beginsWithConsonant(str)) {8
sb.append(str.substring(1)); // removing the first letter9
sb.append(str.charAt(0)); // appending it to the end14
sb.append("ma"); // appending "ma" to the end of the word (common operation)15
sb.append(temp); // adding one letter 'a' to the end of each word17
// the first word gets "a" added to the end,18
// the second word gets "aa" added to the end,20
temp.append("a"); // increasing the a's for every word21
sb.append(" "); // to put space between words25
.trim(); // using trim() to remove the one extra space from the end of string.28
public boolean beginsWithConsonant(String str) {29
return "aeiou".indexOf(str.toLowerCase().charAt(0)) == -1;