Sunday, 1 March 2020

Chapter 11 // Exercise 7 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 11 // Exercise 7

Modify the program from the previous exercise so that it replaces don't with
do not, can't with cannot, etc.; leaves hyphens within words intact (so that
we get " do not use the as-if rule "); and converts all characters to lower 
case.
Eurgh. He just had to say 'etc'  on the contraction words. There are 17 common words that use the 't contraction and only one of them doesn't have a space (cannot). I'm super lazy, so there is no way I'm checking to see if the second to last character is a single quote and then checking if that word is equal to 1 of 17 contractions. 

I ended up putting the contractions and their counterparts into a text file, then reading them into vectors at the start of the program. When a user comes along and then inputs 'don't' it will then just compare the string against the vector of contractions, if there is a match, the index will be used to return the matching 'normal phrase'. You can find the contractions.txt in the main section of Chapter 11 on the git.

That wasn't even the hardest part. I spent the longest ensuring that hyphens were only removed when not wrapped in quotes and were by themselves.

No comments:

Post a Comment