统计字符串中的单词数目
2017-06-26 14:38
309 查看
//统计字符串中的单词数目——统计字符串中单词的数目,更复杂的话从一个文本中读出字符串并生成单词数目统计结果。 /* Test my2test,12test...?tesst hehe,dsf..e3234...242haha */ //输出结果9 #include <iostream> #include <fstream> #include <string> using namespace std; int CountWords(char* filename) { ifstream file(filename, ios::in); if (!file.is_open()) return -1; string word; int count = 0; int size = 0; int index = 0; int preindex = -1; while (file >> word) { for (index = 0, preindex = -1, size = word.size(); index < size; ++index) { index = word.find_first_of("0123456789,.?", index); if (index == string::npos)//查找结束 { if (preindex == -1 || size > preindex + 1) { ++count; break; } } else { if (preindex == -1 && index != 0 || index > preindex + 1) { ++count; } preindex = index; } } } return count; } int main(int argc, char* argv[]) { cout << CountWords("D:/test.txt") << endl; system("pause"); return 0; }
相关文章推荐
- 统计字符串中的单词数目
- [一天一项目]统计字符串中的单词数目
- 题解:统计字符串中单词数目
- 题解:统计字符串中单词数目
- 模拟基础之统计字符串中的英文单词数目
- 统计字符串中的单词数目
- 输出一行字符串中 所有纯英文单词的数目
- 第十六周项目二 用指针玩转字符串-统计句子中的单词个数
- 第十六周项目2-用指针玩字符串(任务四:统计单词的个数)
- Vijos P1118 统计单词个数(动态规划,划分型DP,字符串)
- 74.统计字符串中各原音字母的数目
- 统计一个字符串中的单词的个数,并打印各个单词
- Java 文件统计:编写程序,统计英文文本文件中的字符数目和单词数目。程序运行时,输入要统计的文件的名称,程序处理后输出字符数目和单词数目
- 统计一段字符串中单词和字母各出现的次数
- C语言实现二叉树-利用二叉树统计单词数目
- PHP函数库06:PHP统计字符串里单词出现次数
- 统计字符串中单词的个数
- java 正则表达式查找某段字符串中所有小写字母开头的单词并统计次数,按出现次数排序
- 第十六周项目二——用指针玩字符串(4.统计句子str中单词的个数)
- 第十六周项目二用指针玩字符串任务四:统计句子中单词的个数(用指针作形参)