5 ways to implement string inversion in Java _java

Source: Internet
Author: User
Tags bitwise stringbuffer

This describes how string inversion is implemented in Java in 5.

One, array implementation string inversion

Array implements string inversion public
  string Reversebyarray () {
    if (str = NULL | | str.length () = 1) {return
      null;
    }
    char[] ch = str.tochararray ();//string converted to 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);
  }

Second, stack implementation string inversion

Using 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 to 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 Top object from this stack return
    new String (CH);
  }

Three, reverse traversal to implement string inversion

Using 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 right to left punctuation character
    }
    retur n sb.tostring ();
  }

Four, bitwise operations to achieve string inversion

Using bitwise operations to implement string inversion public
  string Reversebybit () { 
    if (str = NULL | | str.length () = 1) {return
      null;
    }
    char[] ch = str.tochararray ();//string converted 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);
  }

Recursive implementation of string inversion

Using recursion to implement string inversion public
  string reversebyrecursive (String str) {
    if (str = NULL | | str.length () = 0) {
      return null;
    }
    if (str.length () = = 1) {
      return str;
    } else {
      //starts intercepting the string from subscript 1, returns the character returning
      Reversebyrecursive ( Str.substring (1)) + str.charat (0);
    }
  }

VI. Testing

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

Viii. all code used for string inversion

public class Reverse {private String str = NULL;
  Public Reverse (String str) {this.str = str;
    The//array implements string inversion public string Reversebyarray () {if (str = NULL | | str.length () = 1) {return null;
      } char[] ch = str.tochararray ();//string converted to 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 to New String (CH);
    ///stack 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 to 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 Top object from this stack return new String (CH);
    ///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 from right to left punctuation character} return
  Sb.tostring ();
    ///Use bitwise operation to implement string inversion public string Reversebybit () {if (str = NULL | | str.length () = 1) {return null; 
    } char[] ch = str.tochararray ();//string converted 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 to New String (CH); ///Use recursion to implement String inversion public string reversebyrecursive (String str) {if (str = NULL | | str.length () = 0) {retur
    n null;
    } if (str.length () = = 1) {return str;
    else {//to intercept the string starting with subscript 1, return reversebyrecursive (str.substring (1)) + Str.charat (0) with the character returning subscript 0;  
 }
  }
}

Above this Java in 5 ways to achieve string inversion is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.