Gordonewqewq 2016-04-28 15:55 采纳率: 50%
浏览 1264

c++新手,请大家帮忙看一看代码哪错了

要求:用户输入一段话,输出每个单词出现的次数
如输入:hello my friend . I miss you , my friend
输出:单词 次数
hello 1
my 2
friend 2
I 1
miss 1
you 1

#include
#include
#include

using namespace std;

int main()
{

string sen;//用来记录用户输入的句子
string word[100];
int a,b;
int times;//用来计相同单词的数目

string::size_type first=0,end=0;
int counter=1;


cout<<"请输入一段话:";
getline(cin,sen);
int i=0;//word数组的下标

//将所有标点符号转化为空格
for(int j = 0;j<sen.size();j++)
    {
        if(' ' == sen[j] || ',' == sen[j] || '.' == sen[j] || '!' == sen[j] || ';' == sen[j] || '?' == sen[j])
            sen[j] = ' ';
    }

//用空格作为分隔符将字符串中的单词分隔出来
while(1)
{

    if(((end=sen.find(' ',first)) != string::npos)||((end=sen.find('  ',first)) != string::npos)||((end=sen.find('   ',first)) != string::npos))
    {
        word[i++]=sen.substr(first,end-first);
        first=end+1;
        counter++;
    }


    else
        break;
}
word[i]=sen.substr(first,sen.size()-first);

cout<<"word"<<setw(13)<<"times"<<endl;
cout<<endl;
//对比字符串函数中单词出现的次数
for(a=0;a<counter-1;a++)
{
    times=0;
    for(b=0;b<counter;b++)
    {
        if(word[a] == word[b])
        times++;
    }
}

for(int k=0;k<counter;k++)
{
    cout<<word[k]<<setw(13)<<times<<endl;
}



return 0;

}
我的结果这么是这样啊?
图片说明
哪错了啊?我实在搞不懂了。。大家帮我看一看。。

  • 写回答

6条回答 默认 最新

  • Gordonewqewq 2016-04-28 15:56
    关注

    那三个include是
    #include
    #include
    #include

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题