题目
解答
String toGoatLatin(String S) {if (S == null) {return "";}S = S.trim();if (S.isEmpty()) {return "";}StringBuilder sb = new StringBuilder();String[] tokens = S.split(" ");for (int i = 0, j = 1, length = tokens.length; i < length; ++i, ++j) {StringBuilder field = new StringBuilder();String token = tokens[i];switch (token.charAt(0)) {case 'a':case 'e':case 'i':case 'o':case 'u':case 'A':case 'E':case 'I':case 'O':case 'U': {field.append(token).append("ma");for (int k = 0; k < j; ++k) {field.append("a");}sb.append(field.toString()).append(" ");}break;default: {field.append(token.substring(1)).append(token.charAt(0)).append("ma");for (int k = 0; k < j; ++k) {field.append("a");}sb.append(field.toString()).append(" ");}break;}}if (sb.length() > 0) {sb.deleteCharAt(sb.length() - 1);}return sb.toString();
}
要点
题目比较长,但实际并没有什么难度。