Monday, May 1, 2017

Programming Challenge 10.15 - Character Analysis

Example File: text.txt

/* Character Analysis - This program reads the contents of a file called
    text.txt and determines the following:
   
        * The number of uppercase letters in the file
        * The number of lowercase letters in the file
        * The number of digits in the file */

#include "Utility.h"

/* Reads in the contents of the file 'text.txt',
    stores the contents in a string object */
int getText(string &);

/* Displays the text stored in the string object */
void displayText(const string);

/* Examines the string object, counts every occurence of
   uppercase and lowercase characters as well as digits,
    outputs the result */
void analyzeText(const string);

int main()
{
    int    fRead = 0;
    string fullText;

    fRead = getText(fullText);

    if (fRead == 0)
    {
        displayText(fullText);
        analyzeText(fullText);
    }

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getText

    This function opens and reads in the contents of a file
    called 'text.txt.' The content is read in and stored into
    a string object.
   ********************************************************** */

int getText(string &fullText)
{
    ifstream textFile;
    string   tmp;

    textFile.open("txt.txt");

    if (textFile)
    {
        while (getline(textFile, tmp) && !textFile == '\0')
        {
            fullText.insert(fullText.length(), "\t" + tmp + "\n");
        }   
    }
    else
    {
         cout << "\n\tFile open error: The file 'text.txt' could not be\n"
                << "\topened or processed successfully. Make sure that the\n"
                << "\tfilename is correct and the file is not damaged or has\n"
               << "\tbeen moved from the program folder.\n\n"
               << "\tPress enter to exit this program ...";

         return -1;
    }

    textFile.close();

    return 0;
}

/* **********************************************************
   Definition: displayText

    This function accepts a string object as argument. It
    ouputs the contents to screen.
   ********************************************************** */

void displayText(const string fullText)
{
    for (char i : fullText)
    {
        cout << i;
    }
}

/* **********************************************************
   Definition: analyzeText

    This function accepts a string object as argument. It
    finds and counts all upper- and lowercase characters, as
    well as digits. The result is output to screen.
   ********************************************************** */

void analyzeText(const string fullText)
{
    int lower = 0,
         upper = 0,
         digit = 0;

    for (char i : fullText)
    {
        islower(i) ? lower++ : i;
        isupper(i) ? upper++ : i;
        isdigit(i) ? digit++ : i;
    }

    cout << "\n\tThis text contains " << setw(4) << digit
          << " digits.";
    cout << "\n\tThis text contains " << setw(4) << upper
          << " uppercase characters.\n";
    cout << "\tThis text contains " << lower
          << " lowercase characters.\n";
}

Example Output:





No comments:

Post a Comment