sabertooth09

char counting

Oct 20th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<ctype.h>
  4.  
  5. int main()
  6. {
  7.     char a[1001];
  8.     gets(a);
  9.     int cnt[2001];
  10.     memset(cnt,0,sizeof(cnt));
  11.     int l=strlen(a),i;
  12.     for(i=0; i<l; i++)
  13.         cnt[(int)a[i]]++;
  14.     puts("--by lexicographic order--");
  15.     // by lexicographic order
  16.     for(i=0; i<2001; i++)
  17.     {
  18.         if(cnt[i] && isalpha(i))
  19.         {
  20.             printf("%c had repeated for %d times\n",i,cnt[i]);
  21.         }
  22.     }
  23.     puts("--by string order--");
  24.     //by string order
  25.     for(i=0; i<l; i++)
  26.     {
  27.         if(isalpha(a[i]) && cnt[(int)a[i]])
  28.         {
  29.             printf("%c has repeated for %d times\n",a[i],cnt[(int)a[i]]);
  30.             cnt[(int)a[i]]=0;
  31.         }
  32.     }
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment