アイデア:
文字列を配列に変換し、配列を反転させます
反転した配列を文字列に変えます
パラメーターとして逆部の開始位置と終了位置を渡すだけです
コードコピーは次のとおりです。
class reverse_string {
public static void main(string [] args){
文字列s1 = "java php .net";
文字列S2 = Reversestring(S1);
System.out.println(s2);
}
public static void reversestring(string str、int start、int end){
char [] chs = str.tochararray(); // string variable array
Reversearray(chs、start、end); //配列を反転します
new string(chs); //配列を文字列に変更します
}
public static void reversestring(string str){
Retrun Reversestring(str、0、str.length());
}
public static void Reversearray(char [] arr、int x、int y){
for(int start = x、end = y-1; start <end; start ++、end-){
スワップ(arr、start、end);
}
}
private static void swap(char [] arr、int x、int y){
char temp = arr [x];
arr [x] = arr [y];
arr [y] = temp;
}
}