题目来自<系统程序员成长计划>
作者:李先静.
逐个字符扫描 根据是否为字母决定当前状态 由单词内状态切换到单词外 计数字符加1
#include <iostream> #include <string> #include <vector> using namespace std; string testString = " this's a test,'Hello World!!!' Welcome to the real world, it sucks, but you are gona love it!!!"; enum STATE { STAT_INIT, STAT_IN_WORD, STAT_OUT_WORD }; size_t CountWords(const string& s) { int count = 0; STATE state = STAT_INIT; for (size_t i = 0; i < s.size(); i++) { switch (state) { case STAT_INIT: if (isalpha(s[i])) { state = STAT_IN_WORD; }else { state = STAT_OUT_WORD; } break; case STAT_IN_WORD: if (isalpha(s[i])) { state = STAT_IN_WORD; } else { count++; state = STAT_OUT_WORD; } break; case STAT_OUT_WORD: if (isalpha(s[i])) { state = STAT_IN_WORD; } else { state = STAT_OUT_WORD; } break; default: break; }//switch (state) } if (STAT_IN_WORD == state) { count++; } return count; } int main() { cout << CountWords(testString) << endl; return 0; }
改进版 计数以外还讲单词放入容器
#include <iostream> #include <string> #include <vector> using namespace std; string testString = " this's a test,'Hello World!!!' Welcome to the real world, it sucks, but you are gona love it!!!"; enum STATE { STAT_INIT, STAT_IN_WORD, STAT_OUT_WORD }; size_t CountWords(const string& s) { int count = 0; STATE state = STAT_INIT; for (size_t i = 0; i < s.size(); i++) { switch (state) { case STAT_INIT: if (isalpha(s[i])) { state = STAT_IN_WORD; }else { state = STAT_OUT_WORD; } break; case STAT_IN_WORD: if (isalpha(s[i])) { state = STAT_IN_WORD; } else { count++; state = STAT_OUT_WORD; } break; case STAT_OUT_WORD: if (isalpha(s[i])) { state = STAT_IN_WORD; } else { state = STAT_OUT_WORD; } break; default: break; }//switch (state) } if (STAT_IN_WORD == state) { count++; } return count; } size_t word_segmentation(const string& s, vector<string>& strVec) { STATE state = STAT_INIT; size_t count = 0; size_t subStrStart = string::npos; size_t subStrEnd = string::npos; for (size_t i = 0; i < s.size(); i++) { switch (state) { case STAT_INIT: if (isalpha(s[i])) { subStrStart = i; state = STAT_IN_WORD; } break; case STAT_IN_WORD: if (!isalpha(s[i])){ count++; subStrEnd = i; strVec.push_back(s.substr(subStrStart, subStrEnd- subStrStart)); state = STAT_OUT_WORD; } break; case STAT_OUT_WORD: if (isalpha(s[i])) { subStrStart = i; state = STAT_IN_WORD; } break; default: break; }//switch (state) } if (state == STAT_IN_WORD) { count++; subStrEnd = s.size(); strVec.push_back(s.substr(subStrStart, subStrEnd- subStrStart)); } return count; } int main() { cout << CountWords(testString) << endl; vector<string> strVec; cout << word_segmentation(testString, strVec) << endl; for (vector<string>::iterator it = strVec.begin(); it != strVec.end(); it++) { cout << *it << " "; } cout << endl; return 0; }
运行结果
19
19
this s a test Hello World Welcome to the real world it sucks but y
ou are gona love it
请按任意键继续. . .