首页 > 其他 > 详细

CC150-Array and string 1.2

时间:2015-08-15 06:41:36      阅读:143      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!