Question: Word count in C++please! Many text processing tools like Microsoft Word or Google Docswill count the number of words in text. A word is a

Word count in C++please!

Many text processing tools like Microsoft Word or Google Docswill count the number of "words" in text. A word is a sequence ofany characters other than a space. Write a program that reads aninput 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 variableto keep that count. Initialize it to 0 before the loop, and thenincrement 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 withfalse. Else, set it to true.

  • There are actually three cases to consider in each loopiteration. Either you are at a space (so are not in a word), youare not at a space and you are NOT already in a word (so you founda new word), or you are not at a space and you ARE already in aword (so there's nothing to do). So create an if -- else if -- elsebranch statement.

Step by Step Solution

3.53 Rating (160 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!