牛客题目链接
1. 题目考点
- 字符串翻转
2. 考点解析
- 分离成数组,再翻转每个单词
public String trans(String s, int n) {
// write code here
String[] strs = s.split(" ");
for (int i = 0; i < strs.length; i++) {
String newStr = "";
for (int j = 0; j < strs[i].length(); j++) {
if (strs[i].charAt(j) >= 'a' && strs[i].charAt(j) <= 'z')
newStr += (char)(strs[i].charAt(j) - 32);
else
newStr += (char)(strs[i].charAt(j) + 32);
}
strs[i] = newStr;
}
StringBuilder sb = new StringBuilder();
if (s.charAt(s.length() - 1) == ' ') sb.append(" ");
for (int i = strs.length - 1; i >= 0; i--) {
sb.append(strs[i]).append(" ");
}
sb.delete(sb.length() - 1, sb.length());
return sb.toString();
}
- 两次翻转(待补充)