一段英文中,检测英文单词出现最多的四个,并统计个数

#include "stdio.h"
#include "string.h"
int main(void)
{
    int length=0;
    int i=0,j=1,k=0,L=0;
    int number=1; 
    int kongge[500];
    int count[100]={0}; 
    int count1[100]={0}; 
    int temp=0,Flag=0;
    int count2=0;
    char a[20]={0},b[20]={0};
    char data[1000][20]={0};
    char text[]="Tag collision arbitration is considered as one of the critical issues in RFID system design. In order to further improve the identification efficiency of tag anticollision algorithms in RFID systems, several types of dynamic framed slotted ALOHA anticollision algorithms are analyzed, and a new anti-collision algorithm is proposed. The proposed algorithm has the ability of identifying the time slot distribution which is selected by the tags within a reader interrogation range in advance.";
    kongge[0]=-1;
    length=strlen(text);
    for(i=0;i<length;i++)      //统计空格出现的数组下标 
    {
        if(text[i]==' '||i==length-1)
        {
            kongge[j++]=i;
        }
    }
    for(i=0;i<length;i++)                    //将每一个单词都存入data[]数组里 
    {
        k=0;    
           L=kongge[i+1]-kongge[i]-1;    
           if(L>20)
           break;
    //    printf("~~%d,%d~~",L,kongge[i]);
        for(j=kongge[i]+1;j<kongge[i]+1+L;j++)
        {
            if((text[j]<='Z'&&text[j]>='A')||(text[j]<='z'&&text[j]>='a'))
            {
                b[k++]=text[j];
            }    
        } 
        strcpy(data[number++],b);
     //   printf("<<%d,%s>>\n",number-1,data[number-1]);
        memset(b,0,L);  
    }  
//    printf("%d\n",number-1);
    
    for(i=1;i<number;i++)                          //统计每一个单词出现的次数 
    {
        Flag=0;
        if(i>1)
        {
            for(k=1;k<i;k++)                    //判断该单词在之前是否出现过 
            {
                if(strcmp(data[i],data[k])==0)
                {
                    Flag=1;
                } 
            }
        }
        if(Flag==0)
        {
            for(j=i+1;j<number;j++)
            {
                if(strcmp(data[i],data[j])==0)   //相等 ,记下下标和次数 
                {
                    count[i]++;
                }
            }
            count[i]=count[i]+1;
        //    printf("count[%d]=%d\n",i,count[i]);
            count1[i]=count[i];
        }        
    }
    for(i=1;i<number;i++)                        //将统计的次数排序 
    {    
         for(j=i+1;j<number;j++)
         {
             if(count[i]<count[j])
             {
                 temp=count[i];
                 count[i]=count[j];
                 count[j]=temp;    
             }    
          }    
    }
    Flag=0;
    for(j=1;j<=4;j++)                         //挑出最大的4个数 
    {
        if(j>1)
        {
            if(count[j]==count[j-1])
            {
                Flag=1;
            }    
        }
        if(Flag==0)
        {
            for(i=1;i<number;i++)
            {
                if(count1[i]==count[j])
                {    
                     count2++;
                     if(count2==5)
                    {
                        break;
                    }
                    printf("单词%s,出现%d次\n",data[i],count[j]);    
                }    
            }    
        }
        Flag=0;
            
    }    
}
 

猜你喜欢

转载自blog.csdn.net/baidu_15547923/article/details/84105005