自己实现strcpy函数

    xiaoxiao2021-08-21  110

    #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; }
    转载请注明原文地址: https://ju.6miu.com/read-676806.html

    最新回复(0)