Advertisement
cd62131

AlphabetCount

Apr 15th, 2014
1,969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. int *count(FILE *in) {
  5.   int i, c;
  6.   int *counter = (int *) malloc(26 * sizeof(int));
  7.   for (i = 0; i < 26; i++) counter[i] = 0;
  8.   while ((c = fgetc(in)) != EOF) {
  9.     if (isupper(c)) {
  10.       counter[c - 'A']++;
  11.       continue;
  12.     }
  13.     if (islower(c)) {
  14.       counter[c - 'a']++;
  15.       continue;
  16.     }
  17.   }
  18.   return counter;
  19. }
  20. int main(int argc, char **argv) {
  21.   FILE *in = fopen(argv[1], "r");
  22.   int *counter = count(in);
  23.   fclose(in);
  24.   int i;
  25.   for (i = 0; i < 26; i++) printf("%c %d\n", i + 'A', counter[i]);
  26.   free(counter);
  27.   return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement