Face questions fifty-eight: Flip string

 

Topic One: Flip word order

Method A: First of all the overall flip, then reversed again and then each word
I AM A surper man.
.Nam reprus mA I
man surper I AM.

  static char [] reverse(char[]a,int be,int end) {
         while(be<end) {
              char temp=a[be];
              a[be]=a[end];
              a[end]=temp;
              be++;
              end--;
         }
         return a;
     }
     
     static char [] reversemain(char[]a) {
         if(a==null)
              return null;      
          // flip an entire sentence 
         A = Reverse (A, 0,. 1-a.length );
          int BE = 0, End = 0 ;
          // inverting each word 
         the while (BE <-a.length. 1 ) {
               IF (A [ BE] == '' ) { 
                  BE ++ ; 
                  End ++ ; 
              } 
              the else  IF (a [End] == '' || End == a.length) {
                   // the tail of each word; 
                  end-- ; 
                  A = Reverse (A, BE, End); 
                  BE = ++ End;// skip to the next word 
              }
               the else End ++ ;             
         } 
         return A; 
     }

Title II: Left string rotation : the front of a plurality of characters moved behind
ABCDEFG 2
BA gfedc
CDEFG ab &
simply call for the function of a reverse three times
Note: null pointer input problems, memory problems Subscript out of range

 

Guess you like

Origin www.cnblogs.com/niliuxiaocheng/p/12593380.html