Program: Identifying different kinds of characters




Code below:

#include <iostream> // Program will be using keyboard and screen

using namespace std; // Will be using standard identifiers string, cin, cout, endl

int main()
{
    cout << "Please type in some text:" << endl;
    string sentence;
    getline( cin, sentence );
    cout << endl << "Thanks" << endl << endl;

    // Find out how many character symbols are in the sentence
    unsigned int stringLength = sentence.length();

    // Examine each character and determine what kind it is, then update a total
    char currentChar;
    unsigned int alphaTotal = 0;
    unsigned int numericTotal = 0;
    unsigned int spaceTotal = 0;
    unsigned int punctTotal = 0;

    for ( unsigned int position = 0; position < stringLength; position++ )
    {
        currentChar = sentence.at( position );
        cout << currentChar << " ";

        if ( isupper( currentChar ) )
        {
            cout << "capital letter" << endl;
            alphaTotal++;
        }

        if ( islower( currentChar ) )
        {
            cout << "lower-case letter" << endl;
            alphaTotal++;
        }

        if ( isdigit( currentChar ) )
        {
            cout << "numeric digit" << endl;
            numericTotal++;
        }

        if ( isspace( currentChar ) )
        {
            cout << "space" << endl;
            spaceTotal++;
        }

        if ( ispunct( currentChar ) )
        {
            cout << "punctuation" << endl;
            punctTotal++;
        }


    } // end of the for loop

    // Display the summary of characters that were in the sentence
    cout << endl << "Your sentence contains:" << endl;
    cout << alphaTotal << " letters" << endl;
    cout << numericTotal << " digits" << endl;
    cout << spaceTotal << " spaces" << endl;
    cout << punctTotal << " punctuation symbols" << endl;

    return 0;
}