I need som help with counting words in a string s. count the number of words in a string s. Words are separated by whitespace. Has a sol
istringstream iss(s);
st
It is also possible to use regular expressions:
std::regex rx("(\\w+)(;|,)*");
std::string text = "this;is,a;test";
auto words_begin = std::sregex_iterator(text.begin(), text.end(), rx);
auto words_end = std::sregex_iterator();
auto count = std::distance(words_begin, words_end);
std::cout << "count: " << count << std::endl;
for(auto i = words_begin; i != words_end; ++i)
{
auto match = *i;
std::cout << match[1] << '\n';
}
The output is:
count: 4
this
is
a
test