//Ryan Shahriyarpour CS1A Chapter 10 Question 14
/*****************************************************************************
*
* Word Separator
*
*********************
*
* This program accepts a sentence in which all of the words are run
* together, but the first character of each word is uppercase. It converts
* the sentence to one where the words are separated by spaces and only the
* first word starts with an uppercase letter.
* For example, "StopAndSmellTheRoses." becomes "Stop and smell the roses."
*
* INPUT
* sentence - the run-together sentence entered by the user
*
* OUTPUT
* The sentence with spaces between the words
*
****************************************************************************/
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
// DATA DICTIONARY
// sentence - the run-together sentence entered by the user
// separated - the sentence after the words have been separated
// index - loop counter
string sentence;
string separated;
unsigned int index = 0;
// Get the sentence
cout << "Enter a sentence with the words run together, each word\n"
<< "starting with an uppercase letter: ";
getline(cin, sentence);
// Insert a space before each uppercase letter (except the first
// character) and convert that letter to lowercase
for (index = 0; index < sentence.length(); index++)
{
if (index > 0 && isupper(sentence[index]))
{
separated += ' ';
separated += tolower(sentence[index]);
}
else
separated += sentence[index];
}
// Display the result
cout << "\nSeparated: " << separated << endl;
return 0;
}