// Diego Martinez CSC5 Chapter 10, P. 591, #14
/*******************************************************************************
* SEPERATE AND DISPLAY WORDS
* ______________________________________________________________________________
* This program accepts a sentence where all words are run together and each
* word begins with an uppercase letter. The program separates the words with
* spaces and converts the sentence so that only the first word begins with
* an uppercase letter.
*
* Example:
*
* Input: StopAndSmellTheRoses
* Output: Stop and smell the roses
*
* ______________________________________________________________________________
* INPUT
*
* A sentence with combined words
*
* OUTPUT
*
* Formatted sentence with spaces
*
*******************************************************************************/
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
// Variable declarations
string input;
string output = "";
// User input
cout << "Enter a sentence: ";
cin >> input;
// Process string
for (int i = 0; i < input.length(); i++)
{
// Add space before uppercase letters except first letter
if (isupper(input[i]) && i != 0)
{
output += ' ';
output += tolower(input[i]);
}
else
{
// Keep first letter uppercase, others lowercase
if (i == 0)
{
output += input[i];
}
else
{
output += tolower(input[i]);
}
}
}
// Display result
cout << "Formatted sentence: " << output << endl;
return 0;
}