Pages

Saturday, 28 August 2021

Chapter 20 // Exercise 9 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 20 // Exercise 9

Define a program that counts the number of words in a Document. Provide two versions: one that defines word as " a whitespace-separated sequence of characters" and one that defines word as "a sequence of consecutive alphabetic characters." For example, with the former definition, alpha.numeric and as12b are both single words, whereas with the second definition they are both two words.


This table on character handling functions is extremely useful:

I didn't know that isspace() will count control characters like \n as spaces. It made this exercise a lot simpler.

For the first one I iterated through every character and if the current character was a space then I increased the word count; but only if the previous character was not a space.

For the seconds one I increased the word count if the current character was not an alphabetic character; but only if the previous character was an alphabetic character.

No comments:

Post a Comment