Advertisement
llvlleo1810

Đếm từ xuất hiện nhiều nhất trong chuỗi

Aug 6th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<ctype.h>
  4.  
  5. int main(){
  6.     char s[500],*p[500],*q;
  7.     int a[100],n=0;
  8.     gets(s);
  9.     strlwr(s);
  10.     for(p[n]=strtok(s," ");p[n]!=NULL;p[++n]=strtok(NULL," "));
  11.     int max=1;
  12.     for(int i=0;i<n;i++){
  13.         a[i]=1;
  14.     }
  15.     for(int i=0;i<n;i++){
  16.         int count=1;
  17.         if(a[i]){
  18.             a[i]=0;
  19.             for(int j=i+1;j<n;j++){
  20.                 if(strcmp(p[i],p[j])==0){
  21.                     count++;
  22.                 }
  23.             }
  24.         if(count>max) {
  25.             max=count;
  26.             q=p[i];
  27.         }
  28.         }
  29.     }
  30.     printf("%s %d",q,max);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement