/* wordstat.cpp -- Matt Mahoney, mmahoney@cs.fit.edu, Feb. 8, 2004. wordstat reads text from standard input and reports the number of words, sentences, and paragraphs. Definitions: - A word is any sequence of one or more printable (non-whitespace) characters delimited by whitespace (space, tab, or newline). - A sentence is a punctuation character (. ? or !) followed by whitespace. - A paragraph is any sequence of one or more characters other than newlines delimited by two or more newlines (i.e. one or more blank lines). To run: wordstat < inputfile For example: wordstat This is... a test ! ^D 5 word(s), 2 sentence(s), 2 paragraph(s) (! is a word. The sentences end with ... and !. The paragraphs are "This is..." and "a\ntest !".) */ #include #include using namespace std; int main() { int words=0, sentences=0, paragraphs=0; // counts char c, c1='\n', c2='\n'; // current and previous 2 input characters while (cin.get(c)) { if (isspace(c1) && !isspace(c)) ++words; if (isspace(c) && (c1=='.' || c1=='?' || c1=='!')) ++sentences; if (c2=='\n' && c1=='\n' && c!='\n') ++paragraphs; c2=c1; c1=c; } cout << words << " word(s), " << sentences << " sentence(s), " << paragraphs << " paragraph(s)\n"; return 0; }