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

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5 
 4 
 5 
 6 char *mycpy(char *s1, char *s2)
 7 {
 8     //数组型
 9 /*    int i;
10     while(s2[i] != '\0') {
11         s1[i] = s2[i];
12         i++;
13     } 
14     s1[i] = '\0';
15     return s1;    */
16     //指针型
17     char *p = s1;
18     while(*s2 != '\0') {
19         *s1 = *s2;
20         s1++;
21         s2++;
22     } 
23     *s1 = '\0';
24     return p;
25 }
26 
27 int main()
28 {
29     char s1[100];
30     char s2[100];
31 //    gets(s1);
32 //    gets(s2);
33     fgets(s1, N, stdin);
34     if(s1[strlen(s1) - 1] == '\n') {      // 去掉换行符
35         s1[strlen(s1) - 1] = '\0';   
36     }
37     fflush(stdin);                        //清空缓冲区(具体请看gets和fgets函数的区别38     fgets(s2, N, stdin);
39     if(s2[strlen(s2) - 1] == '\n') {      // 去掉换行符
40         s2[strlen(s2) - 1] = '\0';   
41     }
42     printf("%s", mycpy(s1, s2));
43     
44     return 0;
45 } 

 

posted @ 2014-08-16 21:54  Acalm  阅读(7536)  评论(0编辑  收藏  举报