//count the num of words
#include<iostream>
using namespace std;
unsigned countWords(string str)
{
unsigned int wc = 0;
bool flag = true;
for(char c:str){
cout<<c<<endl;
if(c == ' ' || c == '\t' || c == '\n')
flag = true;
else if(flag){
wc++;
flag = false;
}
}
return wc;
}
int main(void)
{
string str = "One two three\n four\tfive ";
cout<<"No of words : "<<countWords(str);
return 0;
}
//record words
#include<iostream>
#include<vector>
#include<sstream>
using namespace std;
vector<string> recordWords(string str)
{
vector<string> v;
//key steps
stringstream sin(str);
string word;
while(sin>>word)
v.push_back(word);
return v;
}
int main(void)
{
string str = "One two three four five";
vector<string> ans = recordWords(str);
for(string s:ans){
cout<<s<<" ";
}
return 0;
}