编写一个程序实现strcpy函数的功能

时间:2024-01-17 19:03:26
 #include <stdio.h>
#include <string.h>
#define N 5 char *mycpy(char *s1, char *s2)
{
//数组型
/* int i;
while(s2[i] != '\0') {
s1[i] = s2[i];
i++;
}
s1[i] = '\0';
return s1; */
//指针型
char *p = s1;
while(*s2 != '\0') {
*s1 = *s2;
s1++;
s2++;
}
*s1 = '\0';
return p;
} int main()
{
char s1[];
char s2[];
// gets(s1);
// gets(s2);
fgets(s1, N, stdin);
if(s1[strlen(s1) - ] == '\n') { // 去掉换行符
s1[strlen(s1) - ] = '\0';
}
fflush(stdin); //清空缓冲区(具体请看gets和fgets函数的区别
fgets(s2, N, stdin);
if(s2[strlen(s2) - ] == '\n') { // 去掉换行符
s2[strlen(s2) - ] = '\0';
}
printf("%s", mycpy(s1, s2)); return ;
}