两个字符串连接-调用自定义函数 分数 10 作者 崔孝凤 单位 山东理工大学 两个字符串连接,在主函数中输入两个字符串,调用函数myscat完成两个字符串的连接,最后输出连接后的字符串,请在程序空白处填写合适的语句以
时间: 2023-12-26 15:05:58 浏览: 31
完成程序。
```c
#include <stdio.h>
#include <string.h>
void myscat(char dest[], char src[]) {
int len_dest = strlen(dest);
int i;
for (i = 0; src[i] != '\0'; i++) {
dest[len_dest + i] = src[i];
}
dest[len_dest + i] = '\0';
}
int main() {
char str1[100], str2[100];
scanf("%s %s", str1, str2);
myscat(str1, str2);
printf("%s", str1);
return 0;
}
```
相关问题
两个字符串连接-调用自定义函数
假设你要连接两个字符串s1和s2,可以自定义一个函数concatenate(s1, s2),实现字符串的连接操作。具体代码如下:
```
def concatenate(s1, s2):
return s1 + s2
s1 = "Hello"
s2 = "World"
result = concatenate(s1, s2)
print(result)
```
运行结果为:
```
HelloWorld
```
编写自定义函数完成两个字符串的比较功能,要求函数形参采用指针,数据输入输出在主函数中完成
好的,我理解了你的要求。以下是一个示例函数,可以实现比较两个字符串的功能:
```c
#include <stdio.h>
int compare_str(char *str1, char *str2) {
int i = 0;
while (str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
return 0;
}
i++;
}
if (str1[i] == '\0' && str2[i] == '\0') {
return 1;
} else {
return 0;
}
}
int main() {
char str1[100], str2[100];
printf("请输入第一个字符串:");
scanf("%s", str1);
printf("请输入第二个字符串:");
scanf("%s", str2);
if (compare_str(str1, str2)) {
printf("两个字符串相同\n");
} else {
printf("两个字符串不同\n");
}
return 0;
}
```
在这个函数中,我们使用了两个指针参数,分别指向两个需要比较的字符串。在主函数中,我们先从用户输入中获取这两个字符串,然后调用 `compare_str` 函数进行比较。如果返回值为 1,说明两个字符串相同,否则说明不同。
值得注意的是,我们在比较字符串时,需要逐个字符比较,直到两个字符串中有一个到达了结尾。如果两个字符串都到达了结尾,说明它们相同;否则说明它们不同。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)