题目1206:字符串连接

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:5117

解决:2373

题目描述:

不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。

输入:

每一行包括两个字符串,长度不超过100。

输出:

可能有多组测试数据,对于每组数据,
不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。
输出连接后的字符串。

样例输入:
abc def
样例输出:
abcdef
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int StringLen(char *s){
 5     char *t = s;
 6     int len = 0;
 7     while(*t != '\0'){
 8         len++;
 9         t++;
10     }
11     return len;
12 }
13 
14 char *StrCat(char *s1, char *s2){
15     int len1 = StringLen(s1);
16     int len2 = StringLen(s2);
17     char *s = (char*)malloc((len1 + len2 + 1) * sizeof(char));
18     char *t = s, *t1 = s1, *t2 =s2;
19     while(*t1 != '\0'){
20         *(t++) = *(t1++);
21     }
22     while(*t2 != '\0'){
23         *(t++) = *(t2++);
24     }
25     *t = '\0';
26     return s;
27 }
28 
29 int main(){
30     char s1[101], s2[101];
31     while(scanf("%s %s", s1, s2) != EOF){
32         printf("%s\n", StrCat(s1, s2));
33     }
34     //system("pause");
35     return 0;
36 }

 

 
越努力,越幸运