实验8-2-7 字符串的连接(15 分)
本题要求实现一个函数,将两个字符串连接起来。
函数接口定义:
1
| char *str_cat( char *s, char *t );
|
函数str_cat
应将字符串t
复制到字符串s
的末端,并且返回字符串s
的首地址。
裁判测试程序样例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <stdio.h> #include <string.h>
#define MAXS 10
char *str_cat( char *s, char *t );
int main() { char *p; char str1[MAXS+MAXS] = {'\0'}, str2[MAXS] = {'\0'};
scanf("%s%s", str1, str2); p = str_cat(str1, str2); printf("%s\n%s\n", p, str1);
return 0; }
|
输入样例:
输出样例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include <stdio.h> #include <string.h>
#define MAXS 10
char *str_cat( char *s, char *t );
int main() { char *p; char str1[MAXS+MAXS] = {'\0'}, str2[MAXS] = {'\0'};
scanf("%s%s", str1, str2); p = str_cat(str1, str2); printf("%s\n%s\n", p, str1);
return 0; } char *str_cat( char *s, char *t ) { int i, j, index,length; index = strlen(s); length = index+strlen(t); for (i = index, j = 0; i < length; i++, j++) s[i] = t[j]; s[length] = '\0'; return s; }
|