字符串连接

题目截图:

 

思路:

  利用辅助数组,将两个字符串都复制到该数组中即可。

 

代码如下:

 

 1 /*
 2     字符串连接 
 3 */
 4 
 5 #include <stdio.h>
 6 #include <string.h>
 7 #include <math.h>
 8 #include <stdlib.h>
 9 #include <time.h>
10 #include <stdbool.h>
11 
12 int main() {
13     // 将str1和str2拼接到str3 
14     char str1[101], str2[101], str3[202]; 
15     scanf("%s %s", str1, str2);
16     int i, j;
17     for(i=0; i<strlen(str1); ++i) {        // 先复制str1 
18         str3[i] = str1[i];
19     }
20     for(j=0; j<strlen(str2); ++j) {        // 后复制 str2 
21         str3[i++] = str2[j];
22     }
23     str3[i] = '\0';                        // 字符串末尾 
24     printf("%s", str3);                    // 输出字符串 
25 
26     return 0;
27 }

 

posted @ 2018-02-06 16:33  Just_for_Myself  阅读(377)  评论(0编辑  收藏  举报