5 way to implement string inversion

Source: Internet
Author: User
Tags bitwise truncated

Here is a description of how string inversion is implemented in Java 5.

One, array implementation string inversion
//Array Implementation string inversion     PublicString Reversebyarray () {if(str = =NULL|| Str.length () = = 1){            return NULL; }        Char[] ch = str.tochararray ();//converting strings to character arrays         for(inti = 0; i < CH.LENGTH/2; i++){            Chartemp =Ch[i]; Ch[i]= Ch[ch.length-i-1]; Ch[ch.length-I-1] =temp; }        return NewString (CH); }  
Second, stack implementation string inversion
//string inversion with stack implementation     PublicString Reversebystack () {if(str = =NULL|| Str.length () = = 1){            return NULL; } Stack<Character> stack =NewStack<character>(); Char[] ch = str.tochararray ();//converting strings to character arrays         for(Charc:ch) {Stack.push (c);//each character, push stack        }         for(inti = 0; i < ch.length; i++) {Ch[i]= Stack.pop ();//Remove the top object from this stack        }        return NewString (CH); }  
Third, reverse traversal to implement string inversion
//using reverse traversal to implement string inversion     PublicString Reversebysort () {if(str = =NULL|| Str.length () = = 1){            return NULL; } stringbuffer SB=NewStringBuffer ();  for(inti = Str.length ()-1; I >= 0; i--) {sb.append (Str.charat (i));//use StringBuffer to stitch characters from right to left        }        returnsb.tostring (); }
Four-bit operation for string inversion
//using bitwise operations to implement string inversion     PublicString reversebybit () {if(str = =NULL|| Str.length () = = 1){            return NULL; }        Char[] ch = str.tochararray ();//converting strings to character arrays        intLen =str.length ();  for(inti= 0; i< LEN/2; i++) {Ch[i]^= ch[len-1-i]; Ch[len-1-i]^=Ch[i]; Ch[i]^= ch[len-1-i]; }        return NewString (CH); }  
V. Recursive implementation of string inversion
//using recursion to implement string inversion     Publicstring reversebyrecursive (String str) {if(str = =NULL|| Str.length () = = 0){            return NULL; }        if(str.length () = = 1){            returnstr; } Else {            //The string is truncated from subscript 1, and the character labeled 0 is returned.            returnReversebyrecursive (str.substring (1)) + Str.charat (0); }    }  
VI. Testing
 Public class Test {    publicstaticvoid  main (string[] args) {        = "123456";         New  Reverse (s);        System.out.println (R.reversebyarray ());        System.out.println (R.reversebystack ());        System.out.println (R.reversebysort ());        System.out.println (R.reversebybit ());        System.out.println (R.reversebyrecursive (s));            
Vii. Results

Viii. all code for string inversion
 Public classReverse {PrivateString str =NULL;  PublicReverse (String str) { This. str =str; }        //Array Implementation string inversion     PublicString Reversebyarray () {if(str = =NULL|| Str.length () = = 1){            return NULL; }        Char[] ch = str.tochararray ();//converting strings to character arrays         for(inti = 0; i < CH.LENGTH/2; i++){            Chartemp =Ch[i]; Ch[i]= Ch[ch.length-i-1]; Ch[ch.length-I-1] =temp; }        return NewString (CH); }    //string inversion with stack implementation     PublicString Reversebystack () {if(str = =NULL|| Str.length () = = 1){            return NULL; } Stack<Character> stack =NewStack<character>(); Char[] ch = str.tochararray ();//converting strings to character arrays         for(Charc:ch) {Stack.push (c);//each character, push stack        }         for(inti = 0; i < ch.length; i++) {Ch[i]= Stack.pop ();//Remove the top object from this stack        }        return NewString (CH); }    //using reverse traversal to implement string inversion     PublicString Reversebysort () {if(str = =NULL|| Str.length () = = 1){            return NULL; } stringbuffer SB=NewStringBuffer ();  for(inti = Str.length ()-1; I >= 0; i--) {sb.append (Str.charat (i));//use StringBuffer to stitch characters from right to left        }        returnsb.tostring (); }    //using bitwise operations to implement string inversion     PublicString reversebybit () {if(str = =NULL|| Str.length () = = 1){            return NULL; }        Char[] ch = str.tochararray ();//converting strings to character arrays        intLen =str.length ();  for(inti= 0; i< LEN/2; i++) {Ch[i]^= ch[len-1-i]; Ch[len-1-i]^=Ch[i]; Ch[i]^= ch[len-1-i]; }        return NewString (CH); }    //using recursion to implement string inversion     Publicstring reversebyrecursive (String str) {if(str = =NULL|| Str.length () = = 0){            return NULL; }        if(str.length () = = 1){            returnstr; } Else {            //The string is truncated from subscript 1, and the character labeled 0 is returned.            returnReversebyrecursive (str.substring (1)) + Str.charat (0); }    }}   
All Code

5 way to implement string inversion

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.