c语言输入俩个字符串,一个字符串占一行,输出连接后的字符串
时间: 2023-12-26 21:04:47 浏览: 139
以下是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("请输入第一个字符串:");
fgets(str1, 100, stdin); // 使用 fgets 函数输入字符串
printf("请输入第二个字符串:");
fgets(str2, 100, stdin);
str1[strcspn(str1, "\n")] = '\0'; // 去掉输入字符串中的换行符
str2[strcspn(str2, "\n")] = '\0';
strcat(str1, str2); // 使用 strcat 函数连接字符串
printf("连接后的字符串为:%s\n", str1);
return 0;
}
```
这个程序首先使用 `fgets` 函数输入两个字符串(每个字符串占一行),然后使用 `strcspn` 函数找到字符串中的换行符并将其替换为字符串结束符 `\0`,最后使用 `strcat` 函数连接字符串并输出结果。
阅读全文
相关推荐
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)