C++实现统计字符串中的单词数目

#include
#include
using namespace std;
int main() {
	string str;
	cout << "请输入一串字符(以一个空格为分隔不同单词)" << endl;
	getline(cin, str);
	int i = 0, count = 0;
	while (i<=str.length()){
		if (str[i] == ' ')
			count++;
		i++;
	}
	count++;
	cout << "本字符串共有" << count << "个单词" << endl;
	system("pause");
	return 0;
}

统计字符串中单词的数目,更复杂的话从一个文本中读出字符串并生成单词数目统计结果。

你可能感兴趣的:(c++)