作业帮 > 综合 > 作业

统计若干行英文中单词的个数和每个单词出现的次数.

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/15 12:32:51
统计若干行英文中单词的个数和每个单词出现的次数.
[输入]若干行英语.
[输出]单词数量及每个单词出现的次数.
注意是单词,不是字母
例如输入:What is your name my name is
输出:is:2
name:2
what:1
.
总个数:7
不要从其他地方复制,要求自己编写.用C/C++编写.我是初学的,程序易懂,简洁优先采纳.
自己写的!
#include<stdio.h>
#include<string.h>
int main()
{
    char str[500],temp[10];
    char word[50][10],count[50]={0};
    int i=0,j=0,k,t;
    gets(str);
    while(str[i]!='\0')
    {
        if(i==0 && str[0]!=' ')
        {
            sscanf(str,"%s",temp);
            strcpy(word[j],temp);
            count[j++]=1;
        } 
        else if(str[i-1]==' ' && str[i]!=' ')
        {
            sscanf(str+i,"%s",temp);
            for(k=0;k<j;k++)
                if(strcmp(word[k],temp)==0) 
                {
                    count[k]++;
                    break;
                }
            if(k==j)
            {
                strcpy(word[j],temp);
                count[j++]=1;
            }
        }
        i++;
     
    }
\x09for(i=0;i<j-1;i++)
\x09\x09for(k=i+1;k<j;k++)
\x09\x09\x09if(count[i]<count[k])
\x09\x09\x09{
\x09\x09\x09\x09strcpy(temp,word[i]);
\x09\x09\x09\x09strcpy(word[i],word[k]);
\x09\x09\x09\x09strcpy(word[k],temp);
\x09\x09\x09\x09t = count[i];
\x09\x09\x09\x09count[i] = count[k];
\x09\x09\x09\x09count[k] = t;
\x09\x09\x09}
\x09t =0;
    for(i=0;i<j;i++)
    {
        printf("%s:%d\n",word[i],count[i]);
\x09\x09t +=count[i];
    }
    printf("总个数:%d\n",t);
    return 0;
}