Test Questions
// Write a function to flip the string. The flip method is as follows: "I am a student" is reversed to "student a am I" without any library function.
It is said that this question was frequently used in a written test or interview. I just saw this question in my book, and I saw it again in the blog Garden ("Publish a typical interview question ").
The author also suggested that the method is to first reverse the entire string and then reverse the string. For example, first, "I am a student" is reversed to "tneduts a ma I", and then each string (separated by spaces) is reversed once. The idea is that simple. I am a little messy. C ++ did not learn well. Sorry.
Algorithm
1 # Include <stdio. h> 2 3 Void Main () 4 { 5 Char STR [] = " I am a student " ;
7 Printf (STR ); 8 Printf ( " \ N " ); 9 10 Char * P ,* Q; 11 Char Temp; 12 P = q = STR; 14 While (* Q! = ' \ 0 ' ) 15 { 16 Q ++; 17 } 18 Q -- ; 19 While (P <= Q) 20 { 21 Temp = * P; 22 * P = * Q; 23 * Q = Temp; 24 P ++ ; 25 Q -- ; 26 } // Returns the entire string. 27 28 Printf (STR ); 29 Printf ( " \ N " ); 30 31 Q = STR; // Pointer pointing to start position 32 Char * S ,* T; 33 S = T = STR; 34 While (* Q! = ' \ 0 ' ) 35 { 36 If (* Q = ' ' ) 37 { 38 T -- ; 39 While (S <= T) 40 { 41 Temp = * T; 42 * T = * S; 43 * S = Temp; 44 S ++ ; 45 T -- ; 46 } // Reverse local string 47 48 S = q + 1 ; 49 T = Q; 50 } 52 Q ++ ; 53 T ++ ; 54 } 55 56 Printf (STR ); 57 Printf ( " \ N " ); 58 }
Improvement
After running, I found that it was successful.
I am a student and you are a student.
The last string is not processed. Because I follow
If(* Q =' ')
And the end of the last string has no space, but ends with '\ 0.
I am doing this to process the last string.
If(* Q =' '| * (Q +1) ='\ 0') {T--;If(* (Q +1) ='\ 0')//Process the last stringT ++;
It looks a bit strange, but it does.
CodeIt seems you can continue optimization. How can I feel that my own code is so bad.
Code
The complete code is as follows:
Complete code of the last string processed
# Include <stdio. h> Void Main (){ Char STR [] = " You are a student " ; Printf (STR); printf ( " \ N " ); Char * P ,* Q; Char Temp; P = Q = STR; While (* Q! = ' \ 0 ' ) {Q ++ ;} Q -- ; While (P <= Q) {temp = *P; * P = * Q; * Q = Temp; P ++ ; Q -- ;} // Returns the entire string. Printf (STR); printf ( " \ N " ); Char * S; q = P = s = STR; // Pointer pointing to start position While (* Q! = ' \ 0 ' ){ If (* Q = ' ' | * (Q + 1 ) = ' \ 0 ' ) {P -- ; If (* (Q + 1 ) = ' \ 0 ' ) // Process the last string P ++ ; While (S <= P) {temp = * P; * P = * S; * S = Temp; s ++ ; P -- ;} // Reverse local string S = Q + 1 ; P = Q;} Q ++ ; P ++ ;} Printf (STR); printf ( " \ N " );}
In addition 《ProgramThe Code seen in the interview book, but this mainly uses Array Processing, and uses the library function (strlen (), but the idea is similar. For more information, see.
How to Implement the programmer Interview Guide
# Include <iostram> # Include <Stdio. h> Int Main ( Void ){ Int Num =- 12345 , J = 0 , I = 0 , Flag = 0 , Begin, end; Char STR [] = " I am a student " , Temp; j = Strlen (STR )- 1 ; Printf ( " String = % s \ n " , STR ); // The first step is to reverse the entire picture and change the word to "tneduts a ma I" While (J> I) {temp =STR [I]; STR [I] = STR [J]; STR [J] = Temp; j -- ; I ++ ;} Printf ( " String = % s \ n " , STR ); Int I = 0 ; // The second step is partially reversed. If it is not a space, the word is reversed. While (STR [I]) { If (STR [I]! = ' ' ) {Begin = I; While (STR [I] & STR [I]! = ' ' ) {I ++ ;} I = I- 1 ; End =I ;} While (End> Begin) {temp = STR [begin]; STR [begin] = STR [end]; STR [end] = Temp; End -- ; Begin ++ ;} I ++ ;} Printf ( " String = % s \ n " , STR ); Return 0 ;}
Now that you can see it, you should think about it. Upgrade only ..
ReferenceProgrammer Interview Guide (second edition)Development job classical interview questions http://www.cnblogs.com/zhangjing230/archive/2012/05/17/2505711.html