5. String inversion and string Inversion

Source: Internet
Author: User

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

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.