#include <iostream>
#include <sstream>
#include <string>
#include <vector> // for the vector option
using namespace std;
int main()
{
string sentence, word;
// create a string vector to hold the words
vector<string> sV;
cout << "Enter a sentence: ";
getline(cin, sentence);
// put the sentence into a stream
istringstream instr(sentence);
// the >> operator separates the stream at a whitespace
while (instr >> word)
{
cout << word << endl;
// optionally store each word in a string vector
// could use an array, but a vector is easier
sV.push_back(word);
}
// now let's look at the vector
cout << "You typed " << sV.size() << " words:\n";
for(int k = 0; k < sV.size(); k++)
{
cout << sV[k] << endl;
}
cin.sync(); // purge enter
cin.get(); // console wait
return 0;
}
~Admin Edit: Added [code] tags, removed all caps from title, fixed spelling mistakes in title

New Topic/Question
Reply




MultiQuote



|