Page 1 of 1

18.33 Q6: Word count in C++ please! Many text processing tools like Microsoft Word or Google Docs will count the number

Posted: Fri May 20, 2022 5:27 pm
by answerhappygod
18.33 Q6: Word count in C++
please!
Many text processing tools like Microsoft Word or Google Docs
will count the number of "words" in text. A word is a sequence of
any characters other than a space. Write a program that reads an
input line of text, and outputs the number of words in that text.
If the input is "I have a bike." then the output is 4.
Hints:
Use getline to read the line of text
Use a for loop to walk through each character in the string.
Count the words as you go through that loop. You'll need a variable
to keep that count. Initialize it to 0 before the loop, and then
increment it whenever you see a new word.
Use a boolean variable to indicate whether you are currently
"in" a word or not. If you see a space, set that variable with
false. Else, set it to true.
There are actually three cases to consider in each loop
iteration. Either you are at a space (so are not in a word), you
are not at a space and you are NOT already in a word (so you found
a new word), or you are not at a space and you ARE already in a
word (so there's nothing to do). So create an if -- else if -- else
branch statement.