CC150-Array and string 1.2

problem:

Implement a function void reverse(char *str) in C and C++ which reverse a null-terminated string.

The solution:

1. use another pointer end point to  str.

2. move *end to end of str and move to forward pass null.

3. exchage str++ and end--, when str<end.

4. there two pointer, one move from end to begin, another is move from begin to end. 

 

the code:

void reverse(char *str){

char* end = str;

char tmp;

if(str){

while(*end){

   end++;

}

end--;

while(str<end){

    tmp =*str;

    ++str = *end;

    --end = tmp;

 

}

}

 

 

}

posted @ 2015-08-15 02:11  haochen_Mark  阅读(132)  评论(0编辑  收藏  举报