Ideas:
1. First convert the string to an array, and one for the element.
2. Loop through the string, swap all the characters before and after the character position, for example: First and last swap, second and reciprocal third swap, third and reciprocal third swap, until all characters have been swapped.
3. Through the above two steps to get the character sequence completely reversed new string.
Here is the code implementation: 1 2 3 4 5 can be converted to 5 4 3 2 1
1PublicClassReverse {23//public static void Main (string[] args)4PublicString Swap (String str) {5//String str = "1 2 3 4 5";6char[] arr =Str.tochararray ();7int begin=0;int end=arr.length-1;8while (Begin <End) {9Char temp =Arr[begin];Ten Arr[begin] =Arr[end];Arr[end] =Tempbegin++;end--;14}1516//for (int i = 0; i < arr.length-1; i++)17// System.out.println (Arr[i]) return new String ( ARR). ToString (); 19 }20 public static void21 String str = "A b c D" ;22 System.out.println ( reverse (). Swap (str)); Span style= "color: #008080;" >23 }24}
Although the above method can implement string flipping, what if we want to flip how is to you?
In fact, the solution is simple,
With spaces as delimiters, the characters between the spaces are flipped again.
Java Practice-String inversion