|
|
|
|
|
|
|
named inFile. When the entire file has been read, it prints a summary table showing the percentage of characters that were uppercase letters, lowercase letters, decimal digits, blanks, and end-of-sentence punctuation marks (?, !, .). |
|
|
|
|
|
|
|
|
Input: Text in the file inFile. |
|
|
|
|
|
|
|
|
Output: A copy of the text in file inFile, and a table giving the name of each category and what percentage of the total the category represents. |
|
|
|
|
|
|
|
|
Discussion: Doing this task by hand would be tedious but quite straightforward. You would set up five places to make hash marks, one for each of the categories of symbols to be counted. You would then read the text character by character, determine which category to put each character into, and make a hash mark in the appropriate place. |
|
|
|
|
|
|
|
|
As a human, you can look at a character and tell immediately which category to mark. We can simulate this process by using the library functions isupper,islower, and isdigit (described in Chapter 8) along with a Switch statement with branches for the other categories. |
|
|
|
|
|
|
|
|
Initialize counters to zero
Open inFile (and verify success)
Read a character from inFile
WHILE NOT EOF on inFile
Print the character
Increment proper counter
Read a character from inFile
Calculate and print percentages |
|
|
|
|
|
|
|
|
|
At this point, we realize that the instructions do not indicate whether the percentages to be taken are percentages of the total number of characters read, including those that do not fit any of the categories or percentages of the total number of characters that fall into the five categories. We assume that all characters should be counted. We thus add a counter (named leftOverCount) for all characters that do not fall into the five categories. |
|
|
|
|
|