String inversion method summary, string inversion Summary

Source: Internet
Author: User

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?
  1. Public class StringReverse {
  2. Public void swap (char [] arr, int begin, int end ){
  3. While (begin <end ){
  4. Char temp = arr [begin];
  5. Arr [begin] = arr [end];
  6. Arr [end] = temp;
  7. Begin ++;
  8. End --;
  9. }
  10. }
  11. // I love java
  12. Public String swapWords (String str ){
  13. Char [] arr = str. toCharArray ();
  14. Swap (arr, 0, arr. length-1 );
  15. Int begin = 0;
  16. For (int I = 1; I <arr. length; I ++ ){
  17. If (arr [I] = ''){
  18. Swap (arr, begin, I-1 );
  19. Begin = I + 1;
  20. }
  21. }
  22. Return new String (arr );
  23. }
  24. Public static void main (String [] args ){
  25. String str = "I love java ";
  26. System. out. println (new StringReverse (). swapWords (str ));
  27. }
  28. }
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?
  1. Public class Main {
  2. Public static void main (String [] args ){
  3. Pipeline SC = new pipeline (System. in );
  4. String str = SC. nextLine ();
  5. String [] sArr = str. split (""); // I love java
  6. List <String> list = new ArrayList <String> ();
  7. List = Arrays. asList (sArr );
  8. // For (int I = 0; I <sArr. length; I ++ ){
  9. // List. add (sArr [I]);
  10. //}
  11. Collections. reverse (list );
  12. For (String word: list ){
  13. System. out. print (word + "");
  14. }
  15. }
  16. }
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?
  1. Public static String reverse (String str ){
  2. Return new StringBuilder (str). reverse (). toString ();
  3. }
public static String reverse(String str){return new StringBuilder(str).reverse().toString();}



[Java]View plaincopyprint?
  1. @ Test
  2. Public void test09 (){
  3. String str = "hello ";
  4. For (int I = str. length ()-1; I> = 0; I --){
  5. Char c = str. charAt (I );
  6. System. out. print (c );
  7. }
  8. }
@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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.