c语言中字符串的复制
c语言中字符串的复制。
1、自定义函数
#include <stdio.h> char *str_copy(char *d, char *s) { char *t = d; while(*d++ = *s++) ; return t; } int main(void) { char str[128] = "ABCDEFG"; char tmp[128]; printf("str: %s\n", str); printf("tmp: "); scanf("%s", tmp); printf("str: %s\n", str_copy(str, tmp)); return 0; }

2、strcpy函数
#include <stdio.h> #include <string.h> // strcpy函数的头文件。 int main(void) { char str[128] = "abcde"; char tmp[128]; printf("str: %s\n", str); printf("tmp: "); scanf("%s", tmp); printf("str: %s\n", strcpy(str, tmp)); return 0; }


浙公网安备 33010602011771号