Ideas:
Turn string into array and invert array
Turn the inverted array into a string
Just pass the start and end positions of the inverted part as parameters
The code copy is as follows:
class reverse_String{
public static void main (String[] args){
String s1 = " java php .net ";
String 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);//Invert the array
retrun new String(chs);//Change the array into a string
}
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--){
swap(arr,start,end);
}
}
private static void swap(char[] arr,int x ,int y){
char temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}