首页 > 其他 > 详细

字符串处理 | reverse_copy

时间:2015-10-09 13:53:41      阅读:316      评论:0      收藏:0      [点我收藏+]

题目描述:

写C语言的拷贝函数,要求复制字符串,并且将复制后的字符串逆序
  比如form中是1234, 则to中是4321
  void reverse_copy(char * to,const char * form)
  不能使用库函数 不能定义其他的变量

不能定义其他变量,也不能用库,就只能用利用栈空间了,代码如下:

 1 /* reverse_copy without null */
 2 char *reverse_copy_noend(char *to, char *from)
 3 {
 4     if(*from == \0) return to;
 5 
 6     to = reverse_copy_noend(to, from+1);
 7     *to = *from;
 8 
 9     return ++to;
10 }
11 
12 void reverse_copy(char *to, char *from)
13 {
14     if(to == 0 || from == 0) return;
15 
16     to = reverse_copy_noend(to, from);
17     *to = \0;
18 }

 

抛去限制的话:

 

技术分享
 1 void reverse_copy(char *to, char *from)
 2 {
 3     if(to == 0 || from == 0) return;
 4 //    to = reverse_copy_noend(to, from);
 5 //    *to = ‘\0‘;
 6     //register
 7     char *from_end = from + strlen(from);   // 通常会给 from_end
 8 
 9     do {
10         *to++ = *from_end;
11     } while(from_end-- != from);
12 }
View Code

 

字符串处理 | reverse_copy

原文:http://www.cnblogs.com/excavator/p/4863701.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!