I am confused about how to write a program that reverse the phrase but maintain the index chosen. Like this,
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();
}
}
the choosen index is C. i want to keep the C in the places, but the other alphabet is reverse.
the output display is
eH si esenihC