自己实现strcpy函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//source是源字符串,desc是目的字符串

//字符串从源字符串拷贝到目的字符串

void silence_strcpy(char *desc, char *source)

{

//养成一个好习惯,判断主调函数分配的内存是否为空

    if (desc == NULL || source == NULL)
    {
        printf("desc == NULL || source == NULL\n");
    }

//下面是2中方法从源字符串拷贝到目标字符串的方法
    /*
    while ((*desc = *source))
    {
        desc++;
        source++;
        if (*source == '\0')
            break;
    }
    */

 //拷贝字符串
    while ((*desc++ = *source++) != '\0')
    {
        ;
    }
    //*desc = '\0';
}


int main()
{
    char source[100];
    char desc[100];

//当输入字符串"end"时程序退出
    while(1)
    {
        printf("please enter you string ");
        scanf("%s", source);


        if (strncmp(source, "end", 3) == 0)
            break;

        silence_strcpy(desc, source);

        printf("desc: %s\n", desc);

   //养成一种好习惯,把数组清空

        memset(source, 0, sizeof(100));
        memset(desc, 0, sizeof(100));
    }


    return EXIT_SUCCESS;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值