Advertisement
chatchai_j

Character count

Jan 2nd, 2019
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #define BUFSZ   (1024)
  7. #define DEBUG   1
  8.  
  9. int main(void) {
  10.     int i;
  11.     int alphaCount[ 'Z' - 'A' + 1 ];
  12.     int notAlpha = 0;
  13.     char    buf[BUFSZ];
  14.  
  15.     for (i=0;i<sizeof(alphaCount) / sizeof(int);i++)
  16.         alphaCount[i] = 0;
  17.  
  18.     fgets(buf, BUFSZ, stdin);
  19.     for (i=0;i<strlen(buf);i++) {
  20.         char c = buf[i];
  21.         if (DEBUG) printf("[%c]", c);
  22.         if (islower(c)) {
  23.             alphaCount[ c - 'a' ]++;
  24.         } else if (isupper(c)) {
  25.             alphaCount[ c - 'A' ]++;
  26.         } else {
  27.             notAlpha++;
  28.         }
  29.     }
  30.     for (i=0;i<sizeof(alphaCount) / sizeof(int);i++) {
  31.         printf("%c : %d\n", 'A' + i, alphaCount[i]);
  32.     }
  33.     printf("not alpha : %d\n", notAlpha);
  34.     exit(0);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement