C strcpy Function: Copy Strings in C

In C language, you can use the strcpy function to copy strings. The prototype of this function is as follows:

char *strcpy(char *dest, const char *src);

In this case, dest is a pointer to the target string, and src is a pointer to the source string to be copied.

For example, here is a simple sample program demonstrating how to copy strings using the strcpy function.

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

int main() {
    char src[] = "Hello, world!";
    char dest[20];

    // 复制字符串
    strcpy(dest, src);

    // 打印复制后的字符串
    printf("源字符串: %s\n", src);
    printf("目标字符串: %s\n", dest);

    return 0;
}

In the above example, we first defined a source string src and a destination string dest. Then we used the strcpy function to copy src into dest. Finally, we printed out the source and destination strings, showing that the destination string successfully copied the content of the source string.

bannerAds