fork(2) download
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4. #include <sstream>
  5. #include <iterator>
  6. #include <cctype>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10. bool isWordSeparator(char & c) {
  11. return c == ' ' || c == '-' || c == '\n' || c == '?' || c == '.' || c == ','
  12. || c == '?' || c == '!' || c == ':' || c == ';';
  13. }
  14.  
  15.  
  16. int main()
  17. {
  18. string s = "ab\nAb!aB?AB:ab.AB;ab\nAB\nZZZZ zzzz Zzzz\nzzzz";
  19.  
  20. transform(s.begin(),s.end(),s.begin(),
  21. [](char c)->char { if(isWordSeparator(c)) return ' '; return tolower(c); });
  22.  
  23. istringstream iss(s);
  24.  
  25. //This will create a set of lowercase tokens, leaving the duplicates out:
  26. set<string> words((istream_iterator<string>(iss)), istream_iterator<string>());
  27.  
  28. cout << "Number of Words: " << words.size() << endl;
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
Number of Words: 2