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

写自己的strcat函数------→mycat

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5
 4 
 5 char *mycat(char *s1, char *s2)
 6 {
 7     //数组型
 8 /*    int i = 0;
 9     while(s1[i] != '\0') {
10         i++;
11     } 
12     int j = 0;
13     while(s2[j] != '\0') {
14         s1[i] = s2[j];
15         i++;
16         j++;
17     }
18     s1[i] = '\0';
19     
20     return s1;   */
21     //指针型
22     char *p = s1;            //定义字符型指针p指向s1 
23     while(*s1 != '\0') {     //让s1指向'\0' 
24         s1++;
25     } 
26     while(*s2 != '\0') {     //让s2连在s1后 
27         *s1 = *s2;
28         s1++;
29         s2++;
30     }
31     *s1 = '\0';              //让s1以'\0'结尾 
32     
33     return p;
34     
35 }
36 
37 int main()
38 {
39     char s1[N];
40     char s2[N];
41     fgets(s1, N, stdin);                    
42     if(s1[strlen(s1) - 1] == '\n') {      // 去掉换行符
43         s1[strlen(s1) - 1] = '\0';   
44     }
45     fflush(stdin);                        //因为上面使用了fgets,这里得清空缓冲区(具体请看gets和fgets函数的区别46     fgets(s2, N, stdin);
47     if(s2[strlen(s2) - 1] == '\n') {      // 去掉换行符
48         s2[strlen(s2) - 1] = '\0';   
49     }
50     printf("%s", mycat(s1, s2));
51 //    printf("%s\n%s", s1, s2);
52     
53     return 0;

 

posted @ 2014-08-13 18:01  Acalm  阅读(6392)  评论(0编辑  收藏  举报