编一程序,将两个字符串连接起来,不要用strcat函数
时间: 2023-12-15 13:56:22 浏览: 30
以下是一个简单的程序,用于将两个字符串连接起来,不使用strcat函数:
```c
#include <stdio.h>
int main() {
char str1[100], str2[100];
int i, j;
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
// find the length of the first string
for (i = 0; str1[i] != '\0'; i++);
// concatenate the second string to the end of the first string
for (j = 0; str2[j] != '\0'; j++) {
str1[i + j] = str2[j];
}
// add the null character to the end of the concatenated string
str1[i + j] = '\0';
printf("Concatenated string: %s", str1);
return 0;
}
```
这个程序使用两个字符数组来存储两个输入的字符串,然后使用两个循环来找到第一个字符串的长度和将第二个字符串连接到第一个字符串的末尾。最后,程序添加一个空字符到连接的字符串的末尾,并将其打印出来。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)