5. String inversion and string Inversion
This section describes how to implement String inversion in Java 5.
I. array-Based String Inversion
// Array implement String inversion public String reverseByArray () {if (str = null | str. length () = 1) {return null;} char [] ch = str. toCharArray (); // string to the character array for (int I = 0; I <ch. length/2; I ++) {char temp = ch [I]; ch [I] = ch [ch. length-i-1]; ch [ch. length-i-1] = temp;} return new String (ch );}Ii. stack implementation String Inversion
// Implement the public String reverseByStack () {if (str = null | str. length () = 1) {return null;} Stack <Character> stack = new Stack <Character> (); char [] ch = str. toCharArray (); // string converted into a character array for (char c: ch) {stack. push (c); // each character, push stack} for (int I = 0; I <ch. length; I ++) {ch [I] = stack. pop (); // remove the top object of the stack} return new String (ch );}Iii. Reverse traversal for String Inversion
// Use reverse traversal to implement String inversion public String reverseBySort () {if (str = null | str. length () = 1) {return null;} StringBuffer sb = new StringBuffer (); for (int I = str. length ()-1; I> = 0; I --) {sb. append (str. charAt (I); // use StringBuffer to splice characters from right to left} return sb. toString ();}Iv. bitwise Operations implement String Inversion
// Use bitwise operations to implement String inversion public String reverseByBit () {if (str = null | str. length () = 1) {return null;} char [] ch = str. toCharArray (); // string to character array int len = str. length (); for (int I = 0; I <len/2; I ++) {ch [I] ^ = ch [len-1-I]; ch [len-1-I] ^ = ch [I]; ch [I] ^ = ch [len-1-I];} return new String (ch );}5. Implement String inversion recursively
// Implement String Inversion Using Recursion public String reverseByRecursive (String str) {if (str = null | str. length () = 0) {return null;} if (str. length () = 1) {return str;} else {// truncates a string starting from the subscript of 1 and returns the return reverseByRecursive (str. substring (1) + str. charAt (0 );}}Vi. Test
public class Test { public static void main(String[] args) { String s = "123456"; Reverse r = 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
8. All codes used for String inversion public class Reverse {private String str = null; public Reverse (String str) {this. str = str;} // returns the String inversion public String reverseByArray () {if (str = null | str. length () = 1) {return null;} char [] ch = str. toCharArray (); // string to the character array for (int I = 0; I <ch. length/2; I ++) {char temp = ch [I]; ch [I] = ch [ch. length-i-1]; ch [ch. length-i-1] = temp;} return new String (ch);} // use the stack to implement String inversion public String reverseByStack () {if (str = null | str. length () = 1) {return null;} Stack <Character> stack = new Stack <Character> (); char [] ch = str. toCharArray (); // string converted into a character array for (char c: ch) {stack. push (c); // each character, push stack} for (int I = 0; I <ch. length; I ++) {ch [I] = stack. pop (); // remove the top object of the stack} return new String (ch);} // use reverse traversal to implement String inversion public String reverseBySort () {if (str = null | str. length () = 1) {return null;} StringBuffer sb = new StringBuffer (); for (int I = str. length ()-1; I> = 0; I --) {sb. append (str. charAt (I); // use StringBuffer to splice characters from right to left} return sb. toString () ;}// returns the String inversion public String reverseByBit () {if (str = null | str. length () = 1) {return null;} char [] ch = str. toCharArray (); // string to character array int len = str. length (); for (int I = 0; I <len/2; I ++) {ch [I] ^ = ch [len-1-I]; ch [len-1-I] ^ = ch [I]; ch [I] ^ = ch [len-1-I];} return new String (ch );} // implement String Inversion Using Recursion public String reverseByRecursive (String str) {if (str = null | str. length () = 0) {return null;} if (str. length () = 1) {return str;} else {// truncates a string starting from the subscript of 1 and returns the return reverseByRecursive (str. substring (1) + str. charAt (0 );}}}All code