我想从标准输入中读取一个单词,然后在 C++ 中找出它的长度。我目前正在使用:
char str[80];
cout << "Enter your word: " << endl;
scanf("%s", str);
但无法获取读入的单词长度。
我该怎么做?
谢谢。
最佳答案
查看您如何使用 cout
写入标准输出?以并行方式,在现代 C++ 中,我们使用 cin
从标准输入读取。
此外,在现代 C++ 中,我们有一个真正的字符串类型,称为 std::string
,我们用来存储字符串。它来自 <string>
header ,它可以告诉我们它自己的长度。
因此:
string str;
cout << "Enter your word: " << endl;
cin >> str;
int len = str.length();
关于C++:从标准输入中读取一个单词并找出它的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4394880/