SOURCE FILE: charCount.cpp


//******************************************************************
// CharCounts program
// This program counts the number of letters, periods, question
// marks, and exclamation marks found in the first 100 input
// characters
// Assumption: Input consists of at least 100 characters
//******************************************************************
#include <iostream>
#include <cctype>      // For isalpha()

using namespace std;

void IncrementCounter( char, int&, int&, int&, int& );
void PrintCounters( int, int, int, int );

int main()
{
    char inChar;            // Current input character
    int  loopCount;         // Loop control variable
    int  letterCount = 0;   // Number of letters
    int  periodCount = 0;   // Number of periods
    int  questCount = 0;    // Number of question marks
    int  exclamCount = 0;   // Number of exclamation marks

    cout << "Enter your text:" << endl;
    for (loopCount = 1; loopCount <= 100; loopCount++)
    {
        cin.get(inChar);
        IncrementCounter(inChar, letterCount, periodCount,
                         questCount, exclamCount);
    }
    PrintCounters(letterCount, periodCount, questCount,
                  exclamCount);
    return 0;
}

//******************************************************************

void IncrementCounter(
                /* in */    char ch,            // Current character
                /* inout */ int& letterCount,   // Counter
                /* inout */ int& periodCount,   // Counter
                /* inout */ int& questCount,    // Counter
                /* inout */ int& exclamCount )  // Counter

// Increments the proper counter for a given character, ch

// Precondition:
//     All parameters are assigned
// Postcondition:
//     letterCount == letterCount@entry + 1, if ch == 'A'...'Z'
//  && periodCount == periodCount@entry + 1, if ch == '.'
//  && questCount == questCount@entry + 1, if ch == '?'
//  && exclamCount == exclamCount@entry + 1, if ch == '!'
//  && No counters have been changed, otherwise

{
    if (isalpha(ch))
        letterCount++;
    else
        switch (ch)
        {
            case '.' : periodCount++;
                       break;
            case '?' : questCount++;
                       break;
            case '!' : exclamCount++;
                       break;
            default  : ;            // Unnecessary, but OK
        }
}

//******************************************************************

void PrintCounters( /* in */ int letterCount,    // Counter
                    /* in */ int periodCount,    // Counter
                    /* in */ int questCount,     // Counter
                    /* in */ int exclamCount )   // Counter

// Prints the values of all four counters

// Precondition:
//     All parameters are assigned
// Postcondition:
//     All parameters have been output

{
    cout << endl;
    cout << "Input contained" << endl
         << letterCount << " letters" << endl
         << periodCount << " periods" << endl
         << questCount << " question marks" << endl
         << exclamCount << " exclamation marks" << endl;
}