

求解代码
java
public String trans(String s, int n) {
if (s == null || s.length() == 0) {
return s;
}
char[] str = s.toCharArray();
reverse(str, 0, str.length - 1);
int i = 0, j = 0;
while (i < str.length) {
j = i;
while (j < str.length && str[j] != ' ') {
if (str[j] >= 'a' && str[j] <= 'z') {
str[j] = (char) (str[j] - 'a' + 'A');
} else {
str[j] = (char) (str[j] - 'A' + 'a');
}
j++;
}
reverse(str, i, j - 1);
i = j + 1;
}
return new String(str);
}
public void reverse(char[] arr, int start, int end) {
int i = start;
int j = end;
while (i < j) {
char c = arr[i];
arr[i] = arr[j];
arr[j] = c;
i++;
j--;
}
}
小贴士
代码思路大概是:
整体反转➡️单词遍历➡️大小写互换➕局部反转