1.定义任意两个字符串,完成以下操作: (1)编写求字符串长度函数,显示该字符串长度; (2)编写字符串拼接函数,将该字符串与键盘输入的另一字符串拼接,并显示拼接结果; (3)编写比较大小函数,比较两字符串大小,并显示比较结果。
时间: 2024-05-05 20:20:08 浏览: 12
CSDN开发的ChitGPT,非常乐意回答你的问题!
1. 我可以为您提供如下代码实现操作:
```
#include <iostream>
#include <cstring>
using namespace std;
// 求字符串长度
int length(string str) {
return str.length();
}
// 字符串拼接
string join(string str1, string str2) {
return str1 + str2;
}
// 比较字符串大小
int compare(string str1, string str2) {
return str1.compare(str2);
}
int main() {
// 定义两个字符串
string str1 = "hello";
string str2 = "world";
// 输出字符串长度
cout << "str1长度为:" << length(str1) << endl;
cout << "str2长度为:" << length(str2) << endl;
// 字符串拼接并输出
string str3 = join(str1, str2);
cout << "拼接后的字符串为:" << str3 << endl;
// 比较字符串大小并输出
int result = compare(str1, str2);
if(result < 0) {
cout << "str1小于str2" << endl;
} else if(result == 0) {
cout << "str1等于str2" << endl;
} else {
cout << "str1大于str2" << endl;
}
return 0;
}
```
希望以上代码能够解决您的问题,如有任何疑问,欢迎继续提问!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)