#include <iostream> #include <string> #include <windows.h>
using namespace std; int main() { string line; int i = 0; //访问字符数组的下标 int count = 0; cout << "请输入一句话:"; getline(cin,line); while (line[i] == ' ') i++; //跳过前面的连续空格 while (line[i]) { //while(line[i] != '0') while (line[i] && line[i] != ' ') i++; //跳过连续的多个非空格组合(单词) while (line[i] == ' ') i++; count++; } cout << "一共有" << count << "个单词" << endl; system("pause"); return 0; }