标题:统计字符串中单词总个数以及每个单词出现的个数 ,代码没问题 却无法运行
取消只看楼主
曾是我zjk
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-11-27
结帖率:100%
已结贴  问题点数:20 回复次数:0 
统计字符串中单词总个数以及每个单词出现的个数 ,代码没问题 却无法运行
#include<stdio.h>
#include<string.h>

void main()
{
    char *str="Tom is Tom and you are you";//
    char words[100][20]={0};
    int total=0;//统计总个数
    int wordcount[100]={0};//统计单词频率

    char *p=str;
    char temp[20]={0};
    int tempcount=0;
    int i=0;
    while(p)
    {
        if(*p==' ')
        {
            for(i=0;i<total;++i)
            {
                if(strcmp(words[i],temp)==0)
                {
                    wordcount[i]++;
                }
            }
            if(i==total)
            {
                strcpy(words[total++],temp);
                strcpy(temp,"");
                tempcount=0;
            }
        }
        else
        {
            temp[tempcount++]=*p;
        }
        p++;
    }

    printf("单词总数:%d\n",total);
    for(i=0;i<total;++i)
    {
        printf("单词:%s 数量:%d\n",words[i],wordcount[i]);
    }
}
搜索更多相关主题的帖子: 统计 单词 个数 int temp 
2018-11-27 12:08



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-491598-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.612821 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved