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