learning-cc4e

Solution to https://cc4e.com/. If you copy these you're not right in the head.
git clone https://kaka.farm/~git/learning-cc4e
Log | Files | Refs

main.c (1196B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <ctype.h>
      5 
      6 #include "student.c"
      7 
      8 /**
      9  * The main program to read, parse, and count words
     10  */
     11 int main(void)
     12 {
     13     struct TreeMap * map = TreeMap_new();
     14     struct TreeMapEntry *cur;
     15     struct TreeMapIter *iter;
     16     char word[100];  /* Yes, this is dangerous */
     17     int i,j;
     18     int count, maxvalue;
     19     char *maxkey;
     20 
     21     setvbuf(stdout, NULL, _IONBF, 0);  /* Internal */
     22 
     23     /* Turn off debug */
     24     map->debug = 0;
     25 
     26     /* Loop over each word in the file */
     27     while (scanf("%s", word) != EOF) {
     28         for (i=0, j=0; word[i] != '\0'; i++) {
     29             if ( ! isalpha(word[i]) ) continue;
     30             word[j++] = tolower(word[i]);
     31         }
     32         word[j] = '\0';
     33         count = map->get(map, word, 0);
     34         map->put(map, word, count+1);
     35     }
     36 
     37     maxkey = NULL;
     38     maxvalue = -1;
     39     iter = map->iter(map);
     40     while(1) {
     41         cur = iter->next(iter);
     42         if ( cur == NULL ) break;
     43         if ( maxkey == NULL || cur->value > maxvalue ) {
     44             maxkey = cur->key;
     45             maxvalue = cur->value;
     46         }
     47     }
     48     iter->del(iter);
     49     printf("\n%s %d\n", maxkey, maxvalue);
     50 
     51     map->del(map);
     52 }