//Zachary Abdollahi CS1A Chapter 10, P. 589, #5
/*******************************************************************************
*
* CAPITALIZE SENTENCES IN STRING
*_______________________________________________________________________________
* This program accepts a C-string from the user, passes a
* pointer to the C-string to a function that capitalizes the
* first character of each sentence in the string, and displays
* the modified string.
*_______________________________________________________________________________
* INPUT
* userString : C-string entered by the user
*
* OUTPUT
* userString : Modified C-string with capitalized sentences
*
*******************************************************************************/
#include <iostream>
#include <cctype>
using namespace std;
// Function Prototype
void capitalizeSentences(char *strPtr);
int main ()
{
const int SIZE = 250; // Maximum C-string size
char userString[SIZE]; //INPUT & OUTPUT
// Input String
cout << "Enter a string: ";
cin.getline(userString, SIZE);
// Capitalize First Character of Each Sentence
capitalizeSentences(userString);
// Output Result
cout << "Modified string: " << userString << endl;
return 0;
}
/*******************************************************************************
*
* FUNCTION capitalizeSentences
*_______________________________________________________________________________
* This function accepts a pointer to a C-string and converts
* the first alphabetic character at the start of the string and
* after each sentence-ending punctuation mark (., !, ?) to
* uppercase.
*_______________________________________________________________________________
* INPUT
* strPtr : Pointer to a null-terminated C-string
*
* OUTPUT
* strPtr : Modified C-string in place
*
*******************************************************************************/
void capitalizeSentences(char *strPtr)
{
bool newSentence = true; // Flag indicating start of a new sentence
while (*strPtr != '\0')
{
if (*strPtr == '.' || *strPtr == '!' || *strPtr == '?')
{
newSentence = true;
}
else if (newSentence && isalpha(*strPtr))
{
*strPtr = toupper(*strPtr);
newSentence = false;
}
else if (!isspace(*strPtr))
{
newSentence = false;
}
strPtr++;
}
}