码迷,mamicode.com
首页 > 其他好文 > 详细

CC150-Array and string 1.2

时间:2015-08-15 06:42:04      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

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;

 

}

}

 

 

}

CC150-Array and string 1.2

标签:

原文地址:http://www.cnblogs.com/whaochen/p/4731627.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!