程序实现函数strcpy的功能

程序1 代码如下:

#include <stdio.h>

int main(void)
{
    char a[] = "Linux C Program", b[20], c[20];
    int i;

    for(i = 0; *(a+i) != '\0'; i++)
        *(b+i) = *(a+i);
    *(b+i) = '\0';

    char *p1, *p2;
    p1 = a;
    p2 = c;

    for(; *p1 != '\0'; p1++, p2++)
        *p2 = *p1;
    *p2 = '\0';

    printf("%s\n", a);
    printf("%s\n", b);
    printf("%s\n", c);

    return 0;
}

运行结果:

Linux C Program

Linux C Program

Linux C Program

程序2 代码如下:

#include <stdio.h>

void copy_string1(char src[], char dst[])
{
    int i;

    for(i = 0; src[i] != '\0'; i++)
        dst[i] = src[i];
    dst[i] = '\0';
}
void copy_string2(char *psrc, char *pdst)
{
    for( ; *psrc != '\0'; psrc++, pdst++)
        *pdst = *psrc;
    *pdst = '\0';
}
int main(void)
{
    char a[] = "Linux C Program", b[20], c[20];

    copy_string1(a, b);
    copy_string2(a, c);

    printf("%s\n%s\n%s\n", a, b, c);

    return 0;
}

运行结果:

Linux C Program

Linux C Program

Linux C Program

作者: aaron_xueli   发布时间: 2010-10-21