String inversion method summary, string inversion Summary
Description: Give me a string, for example, I love java. Output: java love I
Method 1
[Java]View plaincopyprint?
- Public class StringReverse {
- Public void swap (char [] arr, int begin, int end ){
- While (begin <end ){
- Char temp = arr [begin];
- Arr [begin] = arr [end];
- Arr [end] = temp;
- Begin ++;
- End --;
- }
- }
- // I love java
- Public String swapWords (String str ){
- Char [] arr = str. toCharArray ();
- Swap (arr, 0, arr. length-1 );
- Int begin = 0;
- For (int I = 1; I <arr. length; I ++ ){
- If (arr [I] = ''){
- Swap (arr, begin, I-1 );
- Begin = I + 1;
- }
- }
- Return new String (arr );
- }
- Public static void main (String [] args ){
- String str = "I love java ";
- System. out. println (new StringReverse (). swapWords (str ));
- }
- }
public class StringReverse {public void swap(char[] arr, int begin, int end) {while(begin < end) {char temp = arr[begin];arr[begin] = arr[end];arr[end] = temp;begin++;end--;}}//I love javapublic String swapWords(String str) {char[] arr = str.toCharArray();swap(arr, 0, arr.length - 1);int begin = 0;for (int i = 1; i < arr.length; i++) {if (arr[i] == ' ') {swap(arr, begin, i - 1);begin = i + 1;}}return new String(arr);}public static void main(String[] args) {String str = "I love java";System.out.println(new StringReverse().swapWords(str));}}
Method 2: (use the method provided by jdk)
[Java]View plaincopyprint?
- Public class Main {
- Public static void main (String [] args ){
- Pipeline SC = new pipeline (System. in );
- String str = SC. nextLine ();
- String [] sArr = str. split (""); // I love java
- List <String> list = new ArrayList <String> ();
- List = Arrays. asList (sArr );
- // For (int I = 0; I <sArr. length; I ++ ){
- // List. add (sArr [I]);
- //}
- Collections. reverse (list );
- For (String word: list ){
- System. out. print (word + "");
- }
- }
- }
public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String str = sc.nextLine();String[] sArr = str.split(" ");//I love javaList<String> list = new ArrayList<String>();list = Arrays.asList(sArr);//for(int i=0;i<sArr.length;i++){//list.add(sArr[i]);//}Collections.reverse(list);for(String word:list){System.out.print(word+" ");}}}
If you change the description, enter a string, such as "abcd" and output "dcba ",
Of course there are many methods. Here I provide a method that comes with jdk, which is relatively simple.
[Java]View plaincopyprint?
- Public static String reverse (String str ){
- Return new StringBuilder (str). reverse (). toString ();
- }
public static String reverse(String str){return new StringBuilder(str).reverse().toString();}
[Java]View plaincopyprint?
- @ Test
- Public void test09 (){
- String str = "hello ";
- For (int I = str. length ()-1; I> = 0; I --){
- Char c = str. charAt (I );
- System. out. print (c );
- }
- }
@Testpublic void test09() {String str = "hello";for (int i = str.length() - 1; i >= 0; i--) {char c = str.charAt(i);System.out.print(c);}}
Copyright statement: I feel very grateful if I still write well, I hope you can use your mouse and keyboard to give me a thumbs up or give me a comment! _______________________________________________________________ You are welcome to reprint. I hope to add the original address while you reprint it. Thank you for your cooperation.