Here's the program. Basically it prompts the user to enter a sentence and uses std::cin to input it. It then displays the sentence as it was entered originally and also it displays it with the spaces between the words removed. For a test input I used "This is a sentence.":
#include <iostream>
#include <string>
using std::endl;
using std::cout;
using std::cin;
using std::string;
int main()
{
string inputString, strWithSpaces, strWithoutSpaces;
cout << "Enter a sentence (ctrl+z when done):" << endl;
while (cin >> inputString)
{
strWithoutSpaces += inputString;
strWithSpaces += (inputString + " ");
}
// Display the concatenatedStrings.
cout << strWithoutSpaces << endl;
cout << strWithSpaces << endl;
return 0;
}
ctrl+z should end the while loop. However the while will not end UNLESS I hit enter before the ctrl+z. If I hit the ctrl+z right after the '.' of the sentence, or after a space after the '.', the while loop keeps going. The while will only end after I enter the ctrl+z after a newline. Why is this?