输入一串字符串,求此字符串的长度。c++代码
时间: 2023-10-08 07:12:52 浏览: 47
可以使用C++中的`string`类型的`length`函数来获取字符串的长度,示例代码如下:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, world!";
int len = str.length();
cout << "The length of the string is: " << len << endl;
return 0;
}
```
输出结果为:
```
The length of the string is: 13
```
相关问题
c++输入一串字符串,求此字符串的长度
您好!感谢您的提问。
在 C++ 中,可以通过以下代码获取输入的字符串并求出其长度:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "请输入字符串:";
getline(cin, str); // 获取输入的字符串
int len = str.length(); // 求字符串长度
cout << "字符串长度为:" << len << endl;
return 0;
}
```
其中,`getline(cin, str)` 可以获取一行输入的字符串,而 `str.length()` 则可以获取字符串的长度。
希望能够帮助到您!
从键盘输入一串字符串,求此字符串的长度。c++代码
可以使用C++中的`string`类型的`length`函数来获取从键盘输入的字符串的长度,示例代码如下:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "Please enter a string: ";
cin >> str;
int len = str.length();
cout << "The length of the string is: " << len << endl;
return 0;
}
```
输入一个字符串后,输出结果为:
```
Please enter a string: Hello, world!
The length of the string is: 13
```