#include //#include #include using namespace std; int count_words(string s) { int words=0; int i = 0; int l = s.length(); while (i < l ) { while ( (i < l) && (s[i] != ' ') ) { i++; } words++; i++; } return words; } int main() { int y; string s; cout << "Please input a sentence: "; getline(cin, s); cout << "Number of words in the sentence: " << count_words(s) << endl; //system("pause"); return 0; }