Saturday, April 29, 2017

Programming Challenge 10.14 - Word Separator

/* Word Separator - This program accepts as input a sentence in which
   all words are run together, but the first character of each word is
    uppercase. The sentence is converted to a string in which the words
    are separated by spaces and only the first word starts with an
    uppercase letter. For example the string "StopAndSmellTheRose."
    Would be converted to "Stop and smell the roses." */

#include "Utility.h"

/* Introduces the functionality of this program to the user */
void intro();

/* Asks the user if he or she wants to repeat the process,
    returns the decision */
char tryAgain();

/* Searches for uppercase characters in a sentence,
   inserts spaces to separate the words at that position */
void separator(string &);

/* Replaces uppercase characters in a sentence with
   their lowercase equivalents */
void senDecapitalizer(string &);

/* Replaces lowercase characters at the beginning of sentences
   with their uppercase equivalent */
void senCapitalizer(string &);

int main()
{
    int     firstRun = 1;
    char   again = ' ';
    string sentence = " ";

    do
    {
        if (firstRun)
        {
            intro();
            firstRun = 0;
        }
        else
        {
            cout << "\n\n\tWORD SEPARATOR\n\n";
        }

        cout << "\tEnter a sentence:\n\t";

        getline(cin, sentence);

        separator(sentence);
        senDecapitalizer(sentence);
        senCapitalizer(sentence);

        cout << "\n\tThis is your sentence, correctly formatted:\n\t";
        cout << sentence;

        again = tryAgain();

        if (again == 'N')
        {
            cout << "\n\tHave a nice day!\n\n";
        }

    } while (again == 'Y');
    pauseSystem();
    return 0;
}

/* **********************************************************
   Definition: intro

    This function introduces the basic functionality of this
    program to the user.
   ********************************************************** */

void intro()
{
    cout << "\n\tWORD SEPARATOR\n\n"
          << "\tEnter a sentence without spaces, each word starting with\n"
          << "\tan uppercase letter, and I will separate the words.\n"
          << "\tA sentence such as:\n\n"
          << "\t'ThisSentenceDoesNotContainAnySpaces'\n\n"
          << "\twill be formatted, to look like this:\n\n"
          << "\t'This Sentence Does Not Contain Any Spaces.'\n\n"
          << "\tYou can repeat this process as often as you like to,\n"
          << "\tas long as you enter 'y' when asked.\n\n";
}

/* **********************************************************
   Definition: tryAgain

    This function asks the user if he or she wishes to try
    again. This decision is returned.
   ********************************************************** */

char tryAgain()
{
    char again = ' ';

    cout << "\n\n\tDo you wish to try this again? ";
    cin >> again;
    cin.ignore();

    /* Input validation */
    while (toupper(again) != 'Y' && toupper(again) != 'N')
    {
        cout << "\n\tDo you wish to try this again? ";
        cin >> again;
        cin.ignore();
    }

    return toupper(again);
}

/* **********************************************************
   Definition: separator

    This function searches for the occurence of uppercase
    characters in a sentence. If found, a space is inserted to
    separate the characters.
   ********************************************************** */

void separator(string &sentence)
{
    for (unsigned int index = 1; index < sentence.length(); index++)
    {
        if (isupper(sentence[index]))
        {
            sentence.insert((index++), " ");
        }
    }
}

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

   This function replaces all uppercase characters found in
    a sentence by their lowercase equivalent.
   ********************************************************** */

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

/* **********************************************************
   Definition: senCapitalizer

   This function converts letters at the beginning of each
    sentence to their uppercase equivalents.
   ********************************************************** */

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

    for (unsigned int index = 0; index < sentence.length(); index++)
    {
        if (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