C语言编程题:(不用C++,用C!)分别找出一个英文句子里出现频率最高和最低的单词(不区分大小写)

C语言编程题:(不用C++,用C!)分别找出一个英文句子里出现频率最高和最低的单词(不区分大小写)假设单词由英文标点“,”“.”“、”或空格区分

#include "stdio.h"
#include <string.h>
struct wn{
int x;
char w[21];
};
int main(int argv,char *argc[]){
char s[]="Moonlight is in front of my bed. I took it for frost on the ground. I lift my eyes to watch the mountain moon. Lower them dream a dream of home.";
char w[21],ch;
int n,i,j,k,ln;
struct wn w_num[100]={0,""};
for(ln=strlen(s);(ch=s[ln]|0x20)<'a' || ch>'z';ln--);
k=i=0;
while(i<ln){
while((ch=s[i]|0x20)<'a' || ch>'z')
i++;
sscanf(s+i,"%[^,. \n]%n",w,&n);
i+=n;
for(j=0;j<k;j++)
if(!stricmp(w,w_num[j].w)){
w_num[j].x++;
break;
}
if(j>=k){
strcpy(w_num[k].w,w);
w_num[k++].x=1;
}
}
for(n=j=w_num[i=0].x;i<k;i++){
if(n<w_num[i].x)
n=w_num[i].x;
if(j>w_num[i].x)
j=w_num[i].x;
}
printf("The highest frequency is %d, they are as follows:\n",n);
for(i=0;i<k;i++)
if(w_num[i].x==n)
printf("%s\n",w_num[i].w);
printf("\nThe lowest frequency is %d, they are as follows:\n",j);
for(i=0;i<k;i++)
if(w_num[i].x==j)
printf("%s\n",w_num[i].w);
return 0;
}

运行结果:

为什么执行结果没有了?再发一遍……

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-02-23
#include <stdio.h>
#include <string.h>

typedef struct  
{
char wd[20];
int count;
}WORDS;

void main()
{
char buf[]="big Big world word Word,you are dog.Big Dog.";
WORDS W[20];
char *str,tmp[20]={0};
int i=0,j=0,k=0,sn=0,flag=0,maxw=0,minw=0,mxp=0,mip=0;
memset(&W,0,sizeof(WORDS)*20);

while (buf[k])
{
if (buf[k]>= 'A' && buf[k]<='Z')
{
buf[k] += 32; 
}
k++;
}
str= buf;
while (*str)
{
if(*str!=',' && *str!='.' && *str!=' '  /*英文中没有顿号*/)
{
tmp[i++] = *str;
}else
{
tmp[i]='\0';
i =0;
flag =0;
for (j=0;j<sn;j++)
{
if (strcmp(tmp,W[j].wd)==0)
{
W[j].count++;
flag = 1;
break;
}
}
if (!flag)
{
strcpy(W[sn].wd,tmp);
W[sn].count++;
sn++;
}
}
str++;
}
for (j=0;j<sn;j++)
{
if (j==0)
{
maxw=minw=W[j].count;
}
if (W[j].count>maxw)
{
maxw = W[j].count;
mxp = j;
}
if (W[j].count<minw)
{
minw = W[j].count;
mip = j;
}
}
printf("use Max word[%s]use[%d]times\nuse Min word[%s]use[%d]times\n",
W[mxp].wd,W[mxp].count,W[mip].wd,W[mip].count);
}
use Max word[big]use[3]times
use Min word[world]use[1]times
Press any key to continue

本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网