/*
Student: Colin Redpath
Class: CSC-134-301
Assignment: Chapter 10 Homework #1
Due Date: 4/28/2017
Description: Sentence Capitalizer (5)
Write a function that accepts a pointer to a C-string as an argument and capitalizes the first character of each sentence in the strPtring. For instance, if the string argument is “hello. my name is Joe. what is your name?” the function should manipulate the strPtring 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 strPtring should be displayed on the screen.
*/
#include <iostream>
#include <string>
#include <string.h> //needed for strlen
#include <cctype> //needed for char testing
#include <iomanip>
using namespace std;
//function prototypes
string capitalizeFirst(char *);
void resetInput();
const int LENGTH = 200; //allocates enough memory for the char array
int main()
{
char input[LENGTH];
//Validate user input
do {
// Get input from the user to test capitalization function
cout << "Enter a few sentences, without capitalizing the first word of each sentence.\n";
cin.getline(input, LENGTH);
if (!isalpha(input, LENGTH))
{
cout << "Whoops! You must enter letters!\n"
<< "Enter a few sentences, without capitalizing the first word of each sentence.\n";
resetInput();
cin.getline(input, LENGTH);
}
}while(!isalpha(input, LENGTH));
cout << capitalizeFirst(input) << endl;
return 0;
}
/********************************************************************
* Function: string capitalizeFirst(char *strPtr)
* Purpose: Function accepts a C-string argument entered by the user. The function
then capitalizes the first word of every sentence, returning the newly formated
* Parameters: char *strPtr - a pointer to a C-string entered by the user.
* Return: Returns the new formated pointer strPtr
********************************************************************/
string capitalizeFirst(char *strPtr)
{
for (int i = 0; i < strlen(strPtr); i++)
{
if(ispunct(strPtr[i]) || i == 0)
{
if (isalpha(strPtr[i]))
{
strPtr[i] = toupper(strPtr[i]);
}
if(isspace(strPtr[i+1]))
{
strPtr[i+2] = toupper(strPtr[i+2]);
i += 2;
}
}
}
return strPtr;
}
/********************************************************************
* Function: void resetInput()
* Purpose: This function may be called to reset the input apparatus
by flushing the cin buffer. Also provides code reuability since
we are testing multiple input queries
* Parameters: none
* Return: none
********************************************************************/
void resetInput()
{
// flush cin's buffer to start next input request with a clean slate
cin.clear();
cin.ignore(100, '\n');
}