// Elaine Torrez CS1A
// _____________________________________________________________
//
// CAPITALIZE SENTENCES IN STRING
// _____________________________________________________________
// This program prompts the user to enter a string containing
// one or more sentences. It then updates the string so that
// the first letter of each sentence is capitalized. The modified
// string is displayed to the user.
// _____________________________________________________________
// INPUT
// userStr : The original sentence(s) entered by the user
//
// OUTPUT
// userStr : The modified string with sentences capitalized
// _____________________________________________________________
#include <iostream>
using namespace std;
// FUNCTION PROTOTYPE
void capitalizeSentences(char *str);
int main()
{
/****************************************************
* VARIABLE DECLARATIONS
****************************************************/
const int SIZE = 300;
char userStr[SIZE]; // INPUT and OUTPUT string
/****************************************************
* INPUT
****************************************************/
cout << "Enter a sentence: ";
cin.getline(userStr, SIZE);
/****************************************************
* PROCESS
****************************************************/
capitalizeSentences(userStr);
/****************************************************
* OUTPUT
****************************************************/
cout << "\nModified sentence:\n";
cout << userStr << endl;
return 0;
}
// *************************************************************
// capitalizeSentences
// -------------------------------------------------------------
// Modifies a C-string so that the first letter of each sentence
// is capitalized. A sentence is considered to end with '.', '?',
// or '!'.
// *************************************************************
void capitalizeSentences(char *str)
{
bool startOfSentence = true;
for (int i = 0; str[i] != '\0'; i++)
{
// Skip leading spaces before a sentence starts
if (startOfSentence && str[i] == ' ')
continue;
// Capitalize the first letter of a sentence
if (startOfSentence && str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32; // Convert to uppercase
startOfSentence = false;
}
// If it's already uppercase, just start sentence tracking
else if (startOfSentence && (str[i] >= 'A' && str[i] <= 'Z'))
{
startOfSentence = false;
}
// Detect sentence-ending punctuation
if (str[i] == '.' || str[i] == '?' || str[i] == '!')
{
startOfSentence = true;
}
}
}