Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. Sentence Capitalizer Write a function that accepts a pointer to a C-string as

ID: 3669920 • Letter: 5

Question

5. Sentence Capitalizer

Write a function that accepts a pointer to a C-string as an argument and 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 should manipulate

the string so it contains “ Hello. My name is Joe. What is your name? ”

Demonstrate the function in a program that asks the user to input a string and then

passes it to the function. The modified string should be displayed on the screen.

Optional Exercise: Write an overloaded version of this function that accepts a string

class object as its argument.

Explanation / Answer

#include <iostream>
#include <cctype>
using namespace std;

/************************************************************************
*            SentenceCapitalizer
* This void function accepts a pointer to a C-string as an argument and
* 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? look out darling!!!" the function will manipulate the
* string so it contains "Hello. My name is Joe. What is your name? look
* out darling!!!"
************************************************************************/
void SentenceCapitalizer(char *strPtr); // Input a string of characters

int main()
{
   const int SIZE = 201; // Array size
   char line[SIZE];      // Input a string of characters

   // Ask the user to input a string of characters
   cout << "Input a string of lower case sentences, "
             << "with each word and sentence separated "
         << "by a single space, and containing no "
         << "more than a total of " << (SIZE-1)
         << " characters (white space characters "
         << "included): ";
   // |^| The last character of SIZE will be NULL
   cin.getline(line, SIZE);

   // Display each sentence capitalized
   SentenceCapitalizer(line);

   return 0;
}

/************************************************************************
* This void function accepts a pointer to a C-string as an argument and
* capitalizes the first character of each sentence in the string.
************************************************************************/
void SentenceCapitalizer(char *strPtr) {

   // Capitalize the first subscript
   *strPtr = toupper(*strPtr);

   // Run while the subscript's
   // character is not NULL
   while (*strPtr != '') {
       // Display the character
       // stored in the current subscript
       cout << *strPtr;
       if (*(strPtr+1) == ' ' && // If next space is a whitespace and
           (*strPtr == '.' ||     // current space is a period or
            *strPtr == '!' ||     // an exclamation point or
            *strPtr == '?')) {    // a question mark
           // Capitalize the subscript
           // after the next subscript
           *(strPtr+2) = toupper(*(strPtr + 2));
      
           // Display the whitespace
           // stored in the following subscript
           cout << *(strPtr+1);
          
           // Continue to the whitespace subscript
           strPtr++;
       }
       // Continue to the next subscript
       strPtr++;
   }
   return;
}

input

Input a string of lower case sentences, with each word and sentence separated by a single space, and containing no more than a total of 200 characters (white
space characters included):                                                                                                                                 
hello. my name is Joe                                                                                                                                       
Hello. My name is Joe

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote