// Timothy Price CS1A Ch 10 Challenge 5
/******************************************************************************
* Format capitalization
*
* This program receives a number of sentences and capitalizes the first letter
* of each sentence.
*
* Input:
* sentence(s)
*
* Output:
* same sentence(s) with reformatted capitalization
******************************************************************************/
#include <cctype>
#include <iostream>
using namespace std;
// Function prototype
void capitalizeSentences(char* str);
int main()
{
// Data dictionary
const int MAX_LENGTH = 256;
char input[MAX_LENGTH];
// Receive input and output confirmation
cout << "Sentence Capitalizer" << endl;
cout << "----------------------------------------" << endl;
cout << "Enter a sentence or sentances: ";
cin.getline(input, MAX_LENGTH);
cout << input << endl;
// Call function
capitalizeSentences(input);
// Output result
cout << "Formatted sentance(s): " << input << endl;
return 0;
}
// Function definition
void capitalizeSentences(char* str)
{
if (str == nullptr)
{
return;
}
bool capitalizeNext = true;
while (*str != '\0')
{
if (capitalizeNext && isalpha(static_cast<unsigned char>(*str)))
{
*str = static_cast<char>(
toupper(static_cast<unsigned char>(*str))
);
capitalizeNext = false;
}
else if (*str == '.' || *str == '!' || *str == '?')
{
capitalizeNext = true;
}
str++;
}
}