1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <vector>
#include <string>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/regex.hpp>
int countWords(const std::string& str) {
std::vector< std::string > result;
static const boost::regex re("[A-Za-z0-9]+");
boost::algorithm::split_regex(result, str, re);
return result.size()-1;
}
int main(int argc, char** argv) {
std::string inputStr = ".. , ! ? Hello,, ,,,,,,, ,, word..! apple orange!";
std::cout << "nw = " << countWords(inputStr) << std::endl;
return 0;
}
|