Monday, July 3, 2017

Programming Challenge 12.7 - Sentence Filter

Example Files: capitalizer.txt
                         capitalized.txt


/* Sentence Filter - This program asks the user for two file names. The
    first is opened for input and the second file is opened for output.
    (It is assumed that the first file contains sentences that end with
    a period.) The program reads the contents of the first file and changes
    all the letters to lowercase except the first letter of each sentence,
    which is made uppercase. The revised contents is stored in the second
    file. */

#include "Utility.h"

int  fileIO();
void senDecapitalizer(string &);
void wordCapitalizer(string &);


int main()
{
    cout << "SENTENCE FILTER\n\n";

    fileIO();

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: fileIO

    This function first attempts to open and read-in text from
    a file. Upon success, the text is processed, and the file
    is closed. Another file is opened for output. If the file
    is created successfully, the processed text is output to
    the file. If one or both file operations fail, an error
    message is displayed, and the program exits.
   ********************************************************** */

int fileIO()
{
    string tmpText = "";
    string sentence = "";

    fstream readFrom;
    fstream writeTo;

    readFrom.open("capitalizer.txt", ios::in);

    if (!readFrom.fail())
    {
        cout << "Reading in contents of text file ...";

        while (getline(readFrom, tmpText))
        {       
            senDecapitalizer(tmpText);
            wordCapitalizer(sentence);

            sentence += tmpText + "\n";
        }
        cout << "\nFile successfully processed.\n\n";
    }
    else
    {
        cout << "File open error. Now exiting the program ...";
        return -1;
    }
    readFrom.close();
   
    cout << "Now closing the file ..\n";

    /* Write data to the file */
    writeTo.open("capitalized.txt", ios::out);

    if (!writeTo.fail())
    {
        cout << "\nWriting text to the file ...";
        writeTo << sentence << " ";

        cout << "\nText successfully written to file.\n";
    }
    else
    {
        cout << "\nFile Writing Error. Unable to create or process file ...\n"
              << "Press enter to exit this program.";
        return -1;
    }
    writeTo.close();

    cout << "\nPress enter to close this program ...";

    return 0;
}

/* **********************************************************
   Definition: senDecapitalizer

   This function accepts a reference to a string object as
    its parameter. It replaces all uppercase characters found
    in a sentence by their lowercase equivalent.
   ********************************************************** */

void senDecapitalizer(string &sentence)
{
    for (unsigned int index = 0; index < sentence.length(); index++)
    {
        if (isupper(sentence[index]))
        {
            sentence[index] = tolower(sentence[index]);
        }
    }
}

/* **********************************************************
   Definition: wordCapitalizer

    This function accepts a reference to a string object as
    its parameter. It turns all letters found at the beginning
    of a sentence to uppercase.
   ********************************************************** */

void wordCapitalizer(string &sentence)
{
    bool isDelim = false;

    for (unsigned int index = 0; index < sentence.length(); index++)
    {
        if (sentence.at(index) == '.' || sentence.at(index) == '!' ||
            sentence.at(index) == '?' || sentence.at(index) == ';')
        {
            isDelim = false;
        }

        if (isalpha(sentence.at(index)) && isDelim == false)
        {
            isDelim = true;
            sentence.at(index) = toupper(sentence.at(index));
        }
    }
}

Example Output:




No comments:

Post a Comment