找出一句话中出现次数最多的单词
2012-09-13 11:53
232 查看
#include <string> #include <iostream> #include <map> using namespace std; int nMax = 0; int j = 0; bool bEnd = false; char buf[20] = {'0'}; bool isWorld(char chr) { if ((chr >= 'a' && chr <= 'z') || (chr >= 'A'&& chr <= 'Z')) return true; return false; } void GetWorld(map<string, int> &test,char chr) { bEnd = false; if(!isWorld(chr)) bEnd =true; if (bEnd && j) { string str; str.assign(buf,j); cout << str << " "; if(test.find(str)==test.end()) { test[str] = 1; } else { test[str]++; } if (nMax < test[str]) nMax = test[str]; memset(buf, '0', sizeof(buf)); j = 0; } if (isWorld(chr)) { buf[j] = chr; j++; } } int main() { char *CWord = ", ,this, ,is a test test hao are you ,"; map<string, int> MapWorld; int i = 0; while(!isWorld(CWord[i])) i++; while (CWord[i]) { GetWorld(MapWorld,CWord[i]); i++; } cout << endl; map<string, int>::iterator itr = MapWorld.begin(); for(itr;itr!=MapWorld.end();itr++) { if(itr->second == nMax) cout<<itr->first<<endl; } getchar(); return 0; }
下面这个类似的题目
有一段文本,统计其中的单词数。例如:
As a technology , "HailStorm" is so new that it is still only known by itscode name.
注意:单词间的间隔不一定是一个空格。
答:可执行程序代码如下,假设该文本已存入text这个数组里。
void main() { char text[1000]={"As a technology , 'HailStorm' is so new that it is still only known by its code name."}; int i=0,count=0; bool flag=true; while (text[i]&&i<1000) { if (text[i]==' ') { flag=true; } else if (flag==true && ((text[i]>='a'&&text[i]<='z')||(text[i]>='A'&&text[i]<='Z'))) { // 前有空格,接着出现字母,表示出现一个单词。 count++; flag=false; } i++; } cout<<count; }
相关文章推荐
- 找出一句话中出现次数最多的单词
- 找出一段文字中出现次数最多的前10个单词以及次数
- 编写一个小程序,从标准输入读入一系列string对象,寻找连续重复出现的单词。程序应该找出满足一下条件的单词:该单词的后面紧接着再次出现自己本身。跟踪重复次数最多的单词及其重复次数,输出.
- 找出指定单词在一句话中出现的次数
- OC 找出在只有空格字母的字符串中出现次数最多的单词
- 针对一个文件 统计其中的单词和字符数 找出文件中出现次数最多的单词数 将文件中出现的单词按频率进行排序并输出
- 找出一个文件中出现次数最多的10个单词
- 编写一个小程序,从标准输入读入一系列string对象,寻找连续重复出现的单词。程序应该找出满足一下条件的单词:该单词的后面紧接着再次出现自己本身。跟踪重复次数最多的单词及其重复次数,输出.
- 找出出现次数最多的单词并输出,并输出出现的次数
- 找出数组里面元素出现次数最多的问题
- 给一段字符串,找出其中出现次数最多的字母及出现的次数(出现最多次数的项只有一个的情况)
- 找出字符串中出现次数最多的字母和出现次数精简版
- 找出字符串中连续出现次数最多的子串
- 找出一堆数里面出现次数最多或最多的前N个数字
- Majority Element 找出一个数组中出现次数最多的元素
- 找出n个字符串中出现次数最多的字符串(创新工场)
- java找出一个数组中出现次数最多的那个元素
- 找出n个字符串中出现次数最多的字符串。
- Linux命令经典面试题:统计文件中出现次数最多的前10个单词
- 找出字符数组中出现次数最多的字符