Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created March 25, 2026 08:07
Show Gist options
  • Select an option

  • Save juanfal/97f155911d91ae16ba8ca1244d477663 to your computer and use it in GitHub Desktop.

Select an option

Save juanfal/97f155911d91ae16ba8ca1244d477663 to your computer and use it in GitHub Desktop.
count words to . all in main
// t04e21.numwords.cpp
// juanfc 2026-03-25
//
#include <iostream>
using namespace std;
int main()
{
int wordCount = 0;
bool inWord = false;
char c;
while (cin.get(c) and c != '.') {
if (c != ' ' and not inWord) {
wordCount++;
inWord = true;
} else if (c == ' ') {
inWord = false;
}
}
cout << wordCount << " words" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment