Monday, April 24, 2017

Programming Challenge 10.10 - Replace Substring Function

/* Replace Substring Function - This program contains a function called
    replaceSubstring. It accepts three string objects as arguments. It
    searches string1 for all occurrences in string2. When it finds an
    occurrence of string2, it replaces it with string3. For example,
    suppose the three arguments have the following values:
  
        * string1:        "the dog jumped over the fence"
        * string2:        "the"
        * string3:        "that"

    With these three arguments, the function returns a string object
    with the value "that dog jumped over the fence." */

#include "Utility.h"

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

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

/* Decapitalizes the first character in a sentence */
void senDecapitalizer(string &, string &);

/* Capitalizes the first character at the beginning of each sentence */
void senCapitalizer(string &);

/* Searches for all occurrences of a word entered by the user in a
    sentence, replaces this word, returns a string object which
    contains the new sentence */
string replaceSubstring(const string, const string, const string);

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

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

        cout << "\n\tEnter a sentence: ";
        getline(cin, sentence);

        cout << "\n\tThis is your original sentence:\n\t"
              << sentence;

        cout << "\n\n\tEnter a word to search for: ";
        getline(cin, searchWord);

        senDecapitalizer(sentence, searchWord);

        cout << "\n\tEnter a word to replace the "
              << "\'" << searchWord << "\' with: ";
        getline(cin, replaceWord);

        altered = replaceSubstring(sentence, searchWord, replaceWord);

        senCapitalizer(altered);

        cout << "\n\tThis is your altered sentence:\n"
              << "\t" << altered << " ";

        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 REPLACER\n\n"
          << "\tThis program first asks you to enter a sentence, then\n"
          << "\tyou are asked to enter a word to be searched for. If the\n"
          << "\tword exists, you are asked to enter a word or words the\n"
          << "\tsearch word should be replaced with. For example, if this\n"
          << "\tis the sentence you entered:\n\n"
          << "\t'The quick brown fox jumped over the lazy dog.'\n\n"
          << "\tIf you search for 'the,' (without quotes), and the word to\n"
          << "\treplace 'the' is 'that', your sentence will be changed to:\n\n"
          << "\t'That quick brown fox jumped over that lazy dog.'\n\n"
          << "\tNote that the input is case-insensitive. You can enter a\n"
          << "\tsentence containing lowercase characters only, or a mixture\n"
          << "\tof both lower and uppercase characters. The same applies to\n"
          << "\tthe word to search for. In other words you can enter:\n\n"
          << "\t'THE', 'tHe' or simply 'the' if you wish to.\n\n"
          << "\tYou can repeat this process by entering 'y' or 'Y', when\n"
          << "\tasked.\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: senCapitalizer

   This function accepts two string class objects. If one or
    the other contains uppercase characters, they are changed
    to lowercase.
   ********************************************************** */

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

/* **********************************************************
   Definition: replaceSubstring

    This function accepts three string objects as argument. It
    searches the original sentence for a string entered by the
    user, and if it is found, replaces the word or words. The
    altered sentence is returned.
   ********************************************************** */

string replaceSubstring(const string sentence, const string searchWord,
                                const string replaceWord)
{
    string altered = sentence;  
    size_t findWord = altered.find(searchWord);

    while (findWord != string::npos)
    {
        altered.replace(findWord, searchWord.size(), replaceWord);
        findWord = altered.find(searchWord, findWord + replaceWord.length());
    }

    return altered;
}

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

   This function accepts a string class object as argument.
   It capitalizes the first letter in each sentence.
   ********************************************************** */

void senCapitalizer(string &sentence)
{
    unsigned int index = 0;
    bool         isDelim = false;

    for (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