写一名为mystrcat的函数,它的功能是将参数2中的字符串接续在参数1的字符串尾部,函数没有返回值。主函数从程序外部接收两个字符串,调用该函数,完成字符串连接并输出连接后的字符串。要求给出mystrcat的两个不同实现版本。\
时间: 2024-03-24 21:39:31 浏览: 13
好的,下面是mystrcat函数的两个不同实现版本:
版本一:
```c
#include <stdio.h>
#include <string.h>
void mystrcat(char *str1, char *str2) {
strcat(str1, str2);
}
int main() {
char str1[100], str2[100];
scanf("%s %s", str1, str2);
mystrcat(str1, str2);
printf("%s", str1);
return 0;
}
```
版本二:
```c
#include <stdio.h>
#include <string.h>
void mystrcat(char *str1, char *str2) {
int len1 = strlen(str1), len2 = strlen(str2);
for (int i = 0; i < len2; i++) {
str1[len1 + i] = str2[i];
}
str1[len1 + len2] = '\0';
}
int main() {
char str1[100], str2[100];
scanf("%s %s", str1, str2);
mystrcat(str1, str2);
printf("%s", str1);
return 0;
}
```
注意:以上两个版本的mystrcat函数没有进行输入合法性检查,如果输入的字符串过长可能会导致溢出。