自定义函数,完成行参1,行参2,把左边的串连接到右边,形成新串,输出一个长串,求出长度用c语言
时间: 2024-03-12 22:46:02 浏览: 8
好的,根据您的要求,我可以为您提供一个C语言的自定义函数,实现将行参1连接到行参2的左边,形成新串,并输出该长串的长度。函数代码如下:
```c
#include <stdio.h>
#include <string.h>
int connect_str(char *str1, char *str2) {
char new_str[1000];
strcpy(new_str, str1);
strcat(new_str, str2);
printf("连接后的字符串为:%s\n", new_str);
int length = strlen(new_str);
printf("连接后的字符串长度为:%d\n", length);
return length;
}
int main() {
// 调用connect_str()函数
int length = connect_str("hello", "world");
// 输出连接后字符串的长度
printf("连接后的字符串长度为:%d\n", length);
return 0;
}
```
您可以将上述代码复制到C语言编译器中运行,然后调用`connect_str()`函数,传入两个字符串参数,即可实现将行参1连接到行参2的左边,形成新串,并输出该长串的长度。例如:
```c
int main() {
// 调用connect_str()函数
int length = connect_str("hello", "world");
// 输出连接后字符串的长度
printf("连接后的字符串长度为:%d\n", length);
return 0;
}
```
运行结果如下:
```
连接后的字符串为:helloworld
连接后的字符串长度为:10
连接后的字符串长度为:10
```
注意,该函数将会输出连接后的字符串和该字符串的长度,并将长度作为返回值返回,您可以根据自己的需要进行调整。同时,为了保证新串不会溢出,我在函数中定义了一个长度为1000的字符数组`new_str`。