生成完整的代码,要求如下:连接两个字符串,不使用 strcat() 。
时间: 2023-06-19 20:09:00 浏览: 54
以下是连接两个字符串的完整代码,不使用 strcat() 函数:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
int i, j;
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
// find the length of first string
i = strlen(str1);
// concatenate second string to first string
for (j = 0; str2[j] != '\0'; i++, j++) {
str1[i] = str2[j];
}
// add null terminator to the concatenated string
str1[i] = '\0';
printf("Concatenated string: %s\n", str1);
return 0;
}
```
在上面的代码中,我们首先声明两个字符串数组 `str1` 和 `str2`,并使用 `scanf` 函数从用户输入中读取这两个字符串。然后,我们使用 `strlen` 函数找到第一个字符串的长度,并使用 `for` 循环将第二个字符串连接到第一个字符串的末尾。最后,我们添加一个空字符作为连接后字符串的结尾,并将其打印到控制台上。
相关推荐
![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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)