Tuesday, April 18, 2017

Programming Challenge 10.5 - Sentence Capitalizer

/* Sentence Capitalizer - This program contains a function that accepts
   a pointer to a C-String object as an argument. It capitalizes the
   first character of each sentence in the string. For instance, if the
   string argument is:
 
      * "hello. my name is Joe. what is your name?"
    
   the function manipulates the string so it contains:
 
      * "Hello. My name is Joe. What is your name?"
 
   This function is demonstrated by asking the user to input a string,
   which is passed to the function. The modified string is displayed
   on the screen.

   Optional Exercise: This program also contains an overloaded version
   of this function that accepts a string class object as its argument. */

#include "Utility.h"

/* Capitalizes the first letter of a word at the beginning of every
   sentence */
void senCapitalizer(char *);

/* Capitalizes the first letter of a word at the beginning of every
   sentence. */
void senCapitalizer(string &);

int main()
{
   const int  NUM_CHARS = 501;
   char          sentence[NUM_CHARS];
   string      ctrlSentence = " ";
 
   cout << "\n\tSENTENCE CAPITALIZER\n\n"
        << "\tEnter a sentence, " << (NUM_CHARS - 1)
        << " characters in length:\n\t";
       cin.getline(sentence, NUM_CHARS);

       cout << "\n\tCapitalizing the letters ...\n\n";
       senCapitalizer(sentence);

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

       cout << "\n\n\tNow enter another sentence:\n\t";
       getline(cin, ctrlSentence);

       cout << "\n\n\tCapitalizing the letters ...\n\n";
       senCapitalizer(ctrlSentence);

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

   pauseSystem();
   return 0;
}

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

   This function accepts a pointer to a C-string object as
   argument. It capitalizes the first letter in each
   sentence.
   ********************************************************** */

void senCapitalizer(char *charPtr)
{
    unsigned int index = 0;
    bool         isDelim = false;

    for (index = 0; index < strlen(charPtr); index++)
    {
        if (charPtr[index] == '.' || charPtr[index] == '!' ||
            charPtr[index] == '?')
        {
            isDelim = false;
        }

        if (isalpha(charPtr[index]) && isDelim == false)
        {
            isDelim = true;
            charPtr[index] = toupper(charPtr[index]);
        }
    }
}

/* **********************************************************
   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