首先需要了解C++输入cin每次只能读入一个(不含空格)字符串且遇到回车键就会结束 #include using namespace std; int main() { string s; while(cin>>s)//遇到回车键就会结束输入,即回车前的单词为我们所统计的最后字符串 cout<<s.length(); return 0; }