1. Normal reverse order
Can arbitrarily request memory or variable, for the pointer version, this method is not good, need to open space within the function, return to the first address of the space before the end of the function, because the memory can not be freed, there is a memory leak, so there is only a reference version:
#define_crt_secure_no_warnings#include<iostream>#include<string>using namespacestd;voidReverse (string&str) { intLen =str.size (); intI, J; Chartmp; for(i =0, j = Len-1; I < J; i++, j--) {tmp=Str[i]; Str[i]=Str[j]; STR[J]=tmp; }}intMainvoid) { stringtmp; CIN>>tmp; Reverse (TMP); cout<< tmp <<Endl; return 0;}
2. String in situ reverse
That is, the idea is to swap the characters on each side of the string in place of the extra memory space. Defines two pointers pointing to the head and tail of the string, respectively.
#define_crt_secure_no_warnings#include<iostream>using namespacestd;Char* Reverse (Char*str) { Char* p = str;//The p pointer points to the string header Char* Q =str; while(*q! =' /') Q++; Q--;//The q pointer points to the tail of the string while(P <q) {Chartemp; Temp= *p; *p++ = *q; *q--=temp; } returnstr; }intMain () {Charstr[ -]; printf ("Please enter a string less than 50: \ n"); Gets (str); cout<< Endl <<"the results in reverse order are:"<< Reverse (str) <<Endl; return 0;}
3. String in reverse order-XOR version
You can use an XOR or exchange value of two variables when you require that you do not use temporary variables. First of all, or operation:
Two variables that participate in an XOR operation, or two if the corresponding bit is the same, the result is 0, otherwise 1.
That
0^0 = 0, 1^0 = 1, 0^1 = 1, 1^1 = 0
It is not difficult to draw a few characteristics of the bitwise XOR:
(1) Any number with a total of 0 xor, unchanged
(2) Any number with a total of 1 XOR, take reverse
(3) Any number that is different from yourself or, put yourself in 0
Similar to the value of two variables without an intermediate variable: a = a + B; b = a–b; A = A–b;
You can also use XOR with the value of two variables without exchanging intermediate variables:
For example, a value of two integer a=10100001,b=00000110 can be exchanged by the following statement:
A = A^b;//a=10100111
b = b^a;//b=10100001
A = A^b;//a=00000110
#define_crt_secure_no_warnings#include<iostream>using namespacestd;Char* Reverse (Char*str) { Char* p =str; Char* Q =str; while(*q! =' /') Q++; Q--; while(P <q) {*p = *p ^ *Q; *q = *p ^ *P; *p = *p ^ *Q; P++; q--; } returnstr;}intMain () {Charstr[ -]; printf ("Please enter a string less than 50: \ n"); Gets (str); cout<< Endl <<"the results in reverse order are:"<< Reverse (str) <<Endl; return 0;}
4. Reverse a sentence by the word
For example, input "I am a student", require output "student a AM I"
Refer to the string in reverse order, it may be possible to reverse the output of the sentence, that is, "I ma a tneduts", you can see exactly the input sentence in the reverse order of each word. Then the problem-solving ideas can be entered in the sentence by the reverse order, and finally the result again in reverse.
#define_crt_secure_no_warnings#include<iostream>using namespacestd;voidReverseword (Char* p,Char*q) { while(P <q) {Chartemp; Temp= *p; *p++ = *q; *q--=temp; }}Char* Reversesentence (Char*str) { Char* p =str; Char* Q =str; /*handle the words in the whole sentence*/ while(*q! =' /') { if(*q = =' ')/*encounters a space: marks the end of the word*/{Reverseword (p, Q-1); P= ++q;/*point to the first letter of the next word*/ } Else{Q++; } } /** Reverse the last word **/Reverseword (p, Q-1); /** End the whole sentence in reverse order **/Reverseword (str, q-1); returnstr;}intMain () {Charstr[ -]; printf ("Please enter a string less than 50: \ n"); Gets (str); cout<< Endl <<"the results in reverse order are:"<< reversesentence (str) <<Endl; return 0;}
5. String Reverse Print
This type of problem is simpler because it doesn't need to be stored, but it's just a little bit of a trick to use recursive invocation and backtracking just the opposite.
Like what:
#define_crt_secure_no_warnings#include<iostream>using namespacestd;//reverse the order of recursive calls and backtrackingvoidprintstring () {Charchar1; scanf ("%c", &char1); if(Char1! ='#') {printstring (); } if(Char1! ='#') {printf ("%c", CHAR1); }}intMain () {printstring (); return 0;}
Read more reference: http://www.cnblogs.com/graphics/archive/2011/03/09/1977717.html
String Reverse Order Summary