单词识别

单词识别

http://www.nowcoder.com/questionTerminal/16f59b169d904f8898d70d81d4a140a0

这题描述错了。。不是按照次数降序,而是单词升序。。。

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

typedef struct{
    char word[20];
    int cnt;
}Word;

int cpr(const void * a,const void* b)
{
    Word * x = (Word *) a;
    Word * y = (Word *) b;
    return strcmp(x->word,y->word);
}
int main()
{
    char str[1000];
    char temp[20];
    Word w[1000];
    int i,j = 0,k = 0,l = 0;
    int flag = 0;//1表示单词存在于字典中,0表示是新单词
    gets(str);
    for(i = 0;i<strlen(str);i++)
    {
        if(isalpha(str[i])) // 是字母
            temp[j++] = tolower(str[i]);
        else if(str[i] == ' ' || str[i] == ',' || str[i] == '.') //单词结束
        {
            temp[j] = '\0';
            j = 0;
            flag = 0;
            if(temp[0]!='\0')//非空单词
            {
                for(k = 0;k<l;k++) // 比较单词是否在字典中
                {
                    if(!strcmp(w[k].word,temp)) // 在字典中
                    {
                        w[k].cnt++;
                        flag = 1;
                        break;
                    }
                }
                if(!flag) // 不在字典中
                {
                    strcpy(w[l].word,temp);
                    w[l].cnt = 1;
                    l++;
                }
            }
            else
                continue;
        }
        else // 其他字符
            temp[j++] = str[i];
    }
    qsort(w,l,sizeof(w[0]),cpr);
    for(i = 0;i<l;i++)
        printf("%s:%d\n",w[i].word,w[i].cnt);
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
06-29 17:30
找实习找着找着就要进入7月了,马上秋招也要开始了,找实习还有意义吗?
绝迹的星:有面就面, 没面上就当日薪4位数大佬免费培训, 面上了再考虑要不要实习
点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务