加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

c++统计单词的个数

(2012-07-09 11:34:07)
标签:

it

分类: c primer
#include <iostream>
#include<vector>
#include<string>
#include<list>
#include<map>
#include<sstream>
#include<fstream>
#include<stdarg.h>
using namespace std;

int main()
{
string s("I was: you, not");
string com(": ,");
string::size_type pos=0,pos2=0;
int cc=0;
//找到前面几个单词,最后一个单词单独处理
cout<<"the word is listed as follows:"<<endl;
while((pos=s.find_first_not_of(com,pos))!=string::npos&&(pos2=s.find_first_of(com,pos2))!=string::npos)
if(pos2>pos)
{
    ++cc;
cout<<s.substr(pos,pos2-pos)<<endl;
    ++pos2;pos=pos2;
}
else
{
     pos2=pos;
    continue;
}
pos=s.find_last_of(com);
if((pos+1)!=string::npos)
cout<<s.substr(pos+1)<<endl;
cout<<"the total number:";
cout<<cc+1<<endl;
    return 0;
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有