- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- string str = "my name,is_zhang.you \\" have'many,apples";
- string separator = ",._\\"' ";
- size_t start = str.find_first_not_of(separator);
- size_t end = 0;
- int wordcount = 0;
- cout << str << endl;
- while(start != string::npos)
- {
- end = str.find_first_of(separator,start + 1);
- if(end == string::npos)
- {
- end = str.length();
- }
- cout << str.substr(start,end - start) << endl;
- start = str.find_first_not_of(separator,end + 1);
- wordcount++;
- }
- cout << "str中共有 " << wordcount << " 个单词" << endl;
- }
- //该片段来自于http://www.codesnippet.cn/detail/290420149399.html
来源: http://www.codesnippet.cn/detail/290420149399.html