1.2 Implement a function void reverse (char *str) in C or C + + which reverses a null-terminated string.
This problem lets us use C + + or C language to flip a string, not a problem, in the previous word reverse Words in a string flipped the words in the strings used in this function, compared with the problem, the problem is simple. C language version is slightly more complex than C + +, should be a string class integrates a lot of useful functions, such as to get the length of the string, the subscript directly access AH and so on, C language implementation should be aware of the first to use a while loop to find the last character, and then go to the middle. See the code below:
C++:
class Solution {public: void reverse (string &s) { int01; while (Left < right ) { char tmp = S[left]; S[left+ +] = S[right]; S[right--] = tmp;}} ;
C
classSolution { Public: voidReverseChar*str) { Char*right =str; if(str) { while(*right) + +Right ; --Right ; while(Str <Right ) { CharTMP = *str; *str++ = *Right ; *right--=tmp; } } }};
[Careercup] 1.2 Reverse string flipping strings