Copy String in C: strcpy Guide

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

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

dest is a pointer to the destination string, and src is a pointer to the source string. Here is a simple example code demonstrating how to use the strcpy function to copy strings.

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

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

    // 使用strcpy函数拷贝字符串
    strcpy(dest, src);

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

    return 0;
}

The code above first defines a source string src and a target string dest. It then uses the strcpy function to copy the content from src string to dest string. Finally, it prints the content of the strings before and after the copy. When running the code above, the output is as follows:

源字符串: Hello, World!
目标字符串: Hello, World!

In this way, the operation of implementing a string copy in C language is completed.

bannerAds