//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;
// Function prototype
string separateWords(const string &text);
int main()
{
// DATA DICTIONARY
// sentence - the run-together sentence entered by the user
// separated - the sentence after the words have been separated
string sentence;
string separated;
// Get the sentence
cout << "Enter a sentence with the words run together, each word\n"
<< "starting with an uppercase letter: ";
getline(cin, sentence);
// Convert and display it
separated = separateWords(sentence);
cout << "\nSeparated: " << separated << endl;
return 0;
}
// Inserts a space before each uppercase letter (except the first character)
// and converts that letter to lowercase.
string separateWords(const string &text)
{
// DATA DICTIONARY
// result - the converted sentence being built
// index - loop counter
string result;
unsigned int index;
for (index = 0; index < text.length(); index++)
{
if (index > 0 && isupper(text[index]))
{
result += ' ';
result += tolower(text[index]);
}
else
result += text[index];
}
return result;
}