Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Statistics The Exploration & Analysis Of Data

Authors: Roxy Peck, Jay L. Devore

7th Edition

0840058012, 978-0840058010

More Books

Students also viewed these Programming questions