Help job: reverse a string (header insertion)

Subject description:

Implemented without the aid of built-in functions to reverse a string 

input 

I am a student. 

Output 
.tneduts a ma I

 

Code:

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int length = str.length();
        String reverse = "";
        for (int i = 0; i < length; i++) {
            reverse = str.charAt(i) + reverse;
        }
        System.out.println(reverse);
    }
}

 

There are a lot more

The method of using a recursive :() 

    public  static String reverse1 (String S) {
       int length = s.length ();
       IF (length <=. 1 )
        return S; 
      String left = s.substring (0, length / 2 ); 
      right string = s.substring (length / 2 , length);
       return reverse1 (right) + reverse1 (left);   // called recursively 
     } 


method of splicing two :( string) 

    public  static string reverse2 (string S) {
       int length = s.length (); 
      String Reverse = "" ;
       for( Int I = 0; I <length; I ++ ) 
       Reverse = s.charAt (I) + Reverse;
       return Reverse; 
     } 


method using three :( array, reverse output) 

    public  static String reverse3 (String S) {
       char [] Array = s.toCharArray (); 
      String Reverse = "" ;
       for ( int I = be array.length -. 1; I> = 0; i-- ) 
       Reverse + = Array [I];
       return Reverse; 
     } 


method using four :( StringBuffer built-in reverse method) 

    public  staticReverse4 String (String S) {
       return  new new . The StringBuffer (S) .reverse () toString (); 
     } 


Method five :( use of temporary variables, two exchange value) 

    public  static String reverse5 (String orig) {
       char [] = S orig .toCharArray ();
       int n-s.length = -. 1 ;
       int halfLength = n-/ 2 ;
       for ( int I = 0; I <= halfLength; I ++ ) {
        char TEMP = S [I]; 
       S [I] = S [n-- I]; 
       S [n- - I] = TEMP; 
      } 
      return  new newString (S); 
     } 


method :( six bits using XOR operation, two data exchange) 

    public  static String reverse6 (String S) { 
      
      char [] = STR s.toCharArray (); 
      
      int the begin = 0 ;
       int End = S. length () -. 1 ;
       the while (the begin < End) { 
       STR [the begin] = ( char ) (STR [the begin] ^ STR [End]); 
       STR [End] = ( char ) (STR [the begin] ^ STR [End ]); 
       STR [the begin] = ( char ) (STR [End] ^ STR [the begin]); 
       the begin ++ ; 
       End -;
      }
      return new String(str);
     }


方法七:(利用栈结构)

    public static String reverse7(String s) {
      char[] str = s.toCharArray();
      Stack<Character> stack = new Stack<Character>();
      for (int i = 0; i < str.length; i++)
       stack.push(str[i]);
      
      String reversed = "";
      for (int i = 0; i < str.length; i++)
       reversed += stack.pop();
      
      return reversed;
     }

 

Guess you like

Origin www.cnblogs.com/haimishasha/p/11354551.html