我对如何编写一个反转短语但保持所选索引的程序感到困惑。像这样,
public static void main(String[] args) {
String string1 = "He is Chinese";
System.out.println(reverse(string1));
}
private static String reverse(String string) {
StringBuilder sb = new StringBuilder();
int length = string.length();
for(int i=0;i<length;i++) {
char a = string.charAt(i);
if(a == ' ') {
sb.append(a);
} else {
int j = i;
while(j < length && string.charAt(j) != ' ') {
j++;
}
sb.append(ReverseString(string.substring(i, j)));
i = j-1;
}
}
return sb.toString();
}
private static String ReverseString(String string) {
StringBuilder sb = new StringBuilder();
for(int i=string.length()-1;i>=0; i--) {
sb.append(string.charAt(i));
}
return sb.toString();
}
}
选择的索引是 C。我想将 C 保留在这些地方,但另一个字母表是相反的。
输出显示是
eH si esenihC