1 # include <iostream> 2/* 3 to implement the inversion function, that is, to reverse a string ending with null. 4 Note: if no additional space is allocated, the string is reversed directly, in addition, pay attention to the concept of null character 6: 7 1. first, calculate the length of the string. The calculated length contains null. Therefore, the calculated value is equal to or equal to 1 8 2. exchange two characters from the beginning and end of the string until the pointer meets 9 */10 11 using namespace STD; 12 Void reverse (char * Str) 13 {14 char * end = STR; 15 char TMP; 16 if (STR) 17 {18 while (* End) 19 {20 + + end; 21} 22 -- end; 23 while (STR <End) 24 {25 TMP = * STR; 26 * STR ++ = * end; 27 * end -- = TMP; 28} 29} 30} 31 32 int main () 33 {34 char s [] = {'A', 'B', 'C'}; 35 cout <S <Endl; 36 reverse (s ); 37 cout <S <Endl; 38 return 0; 39}
Implement the inversion function, that is, to reverse a string ending with null