编一程序,将两个字符串连接起来,不要用strcat函数。
时间: 2023-04-16 11:03:08 浏览: 65
```
#include <iostream>
#include <string>
int main() {
std::string str1, str2;
std::cout << "请输入第一个字符串: ";
std::cin >> str1;
std::cout << "请输入第二个字符串: ";
std::cin >> str2;
std::string result = str1 + str2;
std::cout << "连接后的字符串为: " << result << std::endl;
return 0;
}
```
说明:
- 输入两个字符串
- 使用 + 运算符将两个字符串连接起来
- 输出连接后的字符串
如果你希望将两个字符串连接在一起,并保留两个字符串的原始值,可以使用 += 运算符。
```
std::string str1 = "Hello";
std::string str2 = " World";
str1 += str2;
```
相关问题
编一程序,将两个字符串连接起来,不要用strcat函数
以下是一个简单的程序,用于将两个字符串连接起来,不使用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;
}
```
这个程序使用两个字符数组来存储两个输入的字符串,然后使用两个循环来找到第一个字符串的长度和将第二个字符串连接到第一个字符串的末尾。最后,程序添加一个空字符到连接的字符串的末尾,并将其打印出来。
编一程序,将两个字符串连接起来,用strcat函数;
好的,这个问题很简单。下面是一个使用 strcat 函数的示例程序:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[] = "World!";
strcat(str1, str2);
printf("%s\n", str1);
return 0;
}
```
这个程序会将 "Hello" 和 "World!" 两个字符串连接起来,并输出结果 "HelloWorld!"。
相关推荐
![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)