Summary of string inversion methods

Source: Internet
Author: User

Summary of string inversion methods

Description: Give me a string, for example, I love java. Output: java love I

 

Method 1

 

 
  1. Public class StringReverse {
  2. Public void swap (char [] arr, int begin, int end) {while (begin <end ){
  3. Char temp = arr [begin]; arr [begin] = arr [end];
  4. Arr [end] = temp; begin ++;
  5. End --;}
  6. } // I love java
  7. Public String swapWords (String str) {char [] arr = str. toCharArray ();
  8. Swap (arr, 0, arr. length-1); int begin = 0;
  9. For (int I = 1; I <arr. length; I ++) {if (arr [I] = ''){
  10. Swap (arr, begin, I-1); begin = I + 1;
  11. }}
  12. Return new String (arr );
  13. }
  14. Public static void main (String [] args) {String str = I love java;
  15. System. out. println (new StringReverse (). swapWords (str ));}
  16. }
    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)

     

     

     

     
    1. Public class Main {
    2. Public static void main (String [] args) {nation SC = new Nation (System. in );
    3. String str = SC. nextLine ();
    4. String [] sArr = str. split (); // I love java
    5. List List = new ArrayList ();
    6. List = Arrays. asList (sArr );
    7. // For (int I = 0; I // List. add (sArr [I]); //}
    8. Collections. reverse (list );
    9. For (String word: list ){
    10. System. out. print (word + );}
    11. }}
      Public class Main {public static void main (String [] args) {program SC = new program (System. in); String str = SC. nextLine (); String [] sArr = str. split (); // I love javaList
           
            
      List = new ArrayList
            
             
      (); List = Arrays. asList (sArr); // for (int I = 0; I
             
              

       

       

       

      In addition, if the description is changed, 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.

       

       

      1. Public static String reverse (String str) {return new StringBuilder (str). reverse (). toString ();
      2. }
        public static String reverse(String str){return new StringBuilder(str).reverse().toString();}

         

         

         

        1. @ Test public void test09 (){
        2. String str = hello;
        3. For (int I = str. length ()-1; I> = 0; I --){
        4. Char c = str. charAt (I );
        5. System. out. print (c );}
        6. }
          @Testpublic void test09() {String str = hello;for (int i = str.length() - 1; i >= 0; i--) {char c = str.charAt(i);System.out.print(c);}}

           

Related Article

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.