用c语言实现键盘输入两个字符串并拼接
时间: 2024-05-05 17:17:45 浏览: 19
我可以回答这个问题。您可以使用C语言中的字符串库函数strcat()来拼接两个字符串。首先使用scanf()函数读取两个字符串,然后使用strcat()函数将它们拼接起来。以下是示例代码:
```
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("请输入第一个字符串:");
scanf("%s", str1);
printf("请输入第二个字符串:");
scanf("%s", str2);
strcat(str1, str2);
printf("拼接后的字符串为:%s", str1);
return 0;
}
```
相关问题
从键盘输入两个字符串,将此两个字符串拼成一个字符串C语言
可以使用C语言中的字符串拼接函数strcat()来实现。具体实现代码如下:
```
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], str3[200];
printf("请输入第一个字符串:");
scanf("%s", str1);
printf("请输入第二个字符串:");
scanf("%s", str2);
strcpy(str3, str1);
strcat(str3, str2);
printf("拼接后的字符串为:%s\n", str3);
return 0;
}
```
其中,str1和str2分别用于存储输入的两个字符串,str3用于存储拼接后的字符串。首先使用strcpy()函数将第一个字符串复制到str3中,然后再使用strcat()函数将第二个字符串拼接到str3的末尾。最后输出拼接后的字符串即可。
从键盘输一个字符串,然后再说的字符串中每两个插入一个空格C语言
下面是一个简单的 C 语言程序,它可以让用户从键盘输入一个字符串,并在字符串中每两个字符插入一个空格:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], new_str[200];
int i, j;
printf("请输入一个字符串:");
scanf("%s", str);
// 在字符串中每两个字符插入一个空格
j = 0;
for (i = 0; i < strlen(str); i += 2) {
new_str[j++] = str[i];
new_str[j++] = str[i+1];
new_str[j++] = ' ';
}
new_str[j-1] = '\0';
printf("处理后的字符串为:%s\n", new_str);
return 0;
}
```
这个程序先定义了两个字符串变量 `str` 和 `new_str`,分别用于存储用户输入的字符串和处理后的字符串。然后通过 `scanf` 函数从键盘读入一个字符串,存储在 `str` 变量中。
接着,程序通过一个循环遍历 `str` 中的每两个字符,并将它们拼接起来,中间加一个空格,存储在 `new_str` 变量中。最后,将 `new_str` 输出到屏幕上,就完成了字符串处理的任务。