C++语言MyStrcpy的实现

                                   

                             C++语言MyStrcpy的实现

 

strcpy是一个系统函数,这个系统函数的实现有时候面试会考。

让你不调用任何函数,实现strcpy的功能!

这里就不多说了。直接上代码!

  1 #include <iostream>
  2 #include <string.h>
  3 #include <stdio.h>
  4 using namespace std;
  5 
  6 char *mystrcpy(char *strDest,const char *strSrc)
  7 {
  8         while(*strSrc != '\0')
  9         {
 10                 *strDest = *strSrc;
 11 
 12                 strSrc++;
 13                 strDest++;
 14         }
 15         //若不把最后的'\0'也拷贝,则会把内存中的ASCLL码也输出
 16         *strDest = *strSrc;
 17         return strDest;
 18 }
 19 
 20 int main(int argc, const char *argv[])
 21 {
 22         char a[100];
 23         char b[100];
 24 
 25         while(1)
 26         {
 27                 printf("请输入你想要拷贝的字符串:a=\n");
 28                 scanf("%s", a);
 29 
 30                 printf("---------------------------------------------\n");
 31 
 32                 mystrcpy(b, a);
 33                 printf("调用我的Strcpy输出的结果:b=%s\n", b);
 34 
 35                 strcpy(b, a);
 36                 printf("调用系统strcpy输出的结果:b=%s\n", b);
 37 
 38                 printf("\n");
 39         }
 40         return 0;
 41 }

这个代码已经很详细很具体了!

这里上一张我试验结果的图片。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值