//Sam Partovi CS1A Ch. 10, P.591, #14
/*******************************************************************************
* SEPARATE WORDS WITH SPACES
* ____________________________________________________________
* This program accepts a given string and separates words based on capitalized
* first letters found in the string. The program then displays the separated
* sentence.
* ____________________________________________________________
* INPUT
* userInput : Given string to be separated
* OUTPUT
* stringLength : Length of given string in characters
* formattedString : Separated format of userInput
*******************************************************************************/
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
int main() {
string userInput; //INPUT - Given string to be separated
int stringLength; //INPUT - Length of given string in characters
string formattedString; //INPUT - Separated format of userInput
//Prompt for userInput
cout << "Enter a sentence without spaces: ";
getline(cin, userInput);
//Obtain length of given string
stringLength = userInput.length();
//Iterate through each character in the string
for(int i = 0; i < stringLength; i++) {
//If an uppercase character is found
if(isupper(userInput[i])) {
//Insert a space unless first character
if(i != 0) formattedString += " ";
//Convert character to lowercase
formattedString += (i == 0) ? userInput[i] : tolower(userInput[i]);
}
else formattedString += userInput[i];
}
//Display formatted string
cout << "\n" << formattedString;
return 0;
}