thoga31

Challenge 9 (C)

Aug 13th, 2013
47
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 <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. #define MAX_STR 2
  6. #define ALPHA_LEN 26
  7.  
  8. int main(void) {
  9.     short int sumdigit = 0;
  10.     short int alpha[ALPHA_LEN] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  11.     char s[2];
  12.     int i;
  13.     FILE *f;
  14.    
  15.     f = fopen("challenge9_list.txt", "r");
  16.     while (fscanf(f, "%s", s) != EOF) {
  17.         if (isdigit(s[0])) sumdigit += (short) s[0] - (short) '0';
  18.         else if (isalpha(s[0]))
  19.                 if (islower(s[0])) alpha[(short) s[0] - (short) 'a']++;
  20.     }
  21.     fclose(f);
  22.    
  23.     printf("Sum = %hd\n", sumdigit);
  24.     for(i=0; i < ALPHA_LEN; i++) {
  25.         if (alpha[i] != 0)
  26.             printf("%c = %hd\n", (char) (i + (short) 'a'), alpha[i]);
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment