Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)
1 /*************************************************************************
2 > File Name: ch1.2.cc
3 > Author: purely
4 > Mail: godfantasy#gmail.com
5 > Created Time: 2013年12月24日 15:14:07
6 ************************************************************************/
7
8 #include<iostream>
9 using namespace std;
10
11 void revert(char * s)
12 {
13 char * pheader = s;
14 int length = 0;
15 while(*pheader != '\0')
16 {
17 length++;
18 pheader++;
19 }
20 char *ptail = --pheader;
21 pheader = s;
22 while(pheader < ptail)
23 {
24 *pheader = (*pheader) ^ (*ptail);
25 *ptail = (*pheader) ^ (*ptail);
26 *pheader = (*pheader) ^ (*ptail);
27 pheader++;
28 ptail--;
29 }
30 }
31
32 int main()
33 {
34 char s[] = "abcdefgasdfaiewjkkcnsjkjsjaf;ljafjn";
35 cout << s << endl;
36 revert(s);
37 cout << s << endl;
38 return 0;
39 }
编译器:g++ (GCC) 4.8.2
$ g++ -std=c++11 ch1.2.cc -o ch1.2
$ ./ch1.2
abcdefgasdfaiewjkkcnsjkjsjaf;ljafjn
njfajl;fajsjkjsnckkjweiafdsagfedcba