typedef struct pair{ char word[20]; int num;}pair;
typedef struct mylist{ pair *pairs; int pairnum; }mylist;
void *malloczero(int size){ void *p = malloc(size); memset(p, 0, size); return p;}
void *realloczero(int size){ void *p = realloc(size); memset(p, 0, size); return p;}
void memexch(pair *one, pair *two){ pair tmp; memcpy(&tmp, one, sizeof(pair)); memcpy(one, two, sizeof(pair)); memcpy(two, &tmp, sizeof(pair));}
wordlist wordlist = {NULL, 0, NULL, NULL};
void addpair(char *wordadd){ pair *pair1 = wordlist.pairs; int tmp = wordlist.pairnum; int i = 0; for(i = 0; i < tmp; i++) { if(stricmp(pair1[i].word, wordadd) == 0) { pair1[i].num++; return; } }
pair1 = wordlist.pairs = realloczero((tmp+1)*sizeof(pair)); tmp = ++wordlist.pairnum; strcpy(pair1[tmp-1].word, wordadd); pair1[tmp-1].num++; return;}
void sortpair(){ pair *pair1 = wordlist.pairs; int tmp = wordlist.pairnum; int i = 0; int j = 0;
for(i = 0; i < tmp - 1; i++) for( j = i+1; j < tmp; j++) { if(pair1[i].num < pair1[j].num) { memexch(pair+i, pair+j); } else if(pair1[i].num == pair[j].num) { if(stricmp(pair1[i].word, pair1[j].word) > 0) { memexch(pair+i, pair+j); } } }}
int isdelims(char ch){ if(isalnum(ch)) return 0; if(ch == '-') return 2; return 1;}
int isstrnum(char *str){ int i = 0; for(i = 0; i < strlen(str); i++) { if(isdigit(str[i]) == 0) return 0; } return 1;}
void lowerprint(char *str){ int i = 0; for( i = 0; i < strlen(str); i++) putc(tolower(str[i])); putc('\n');}
int main(){ char tmpword[100]; char tmpch; char *ptmpword = tmpword; int i = 0; FILE *fp = fopen("words.in", "r"); memset(tmpword, 0, 100); while(fp != EOF) { tmpch = fgetc(); if(fp == EOF) break; if(isdelims(tmpch) == 1) { if(tmpword[0] != '\0' && isstrnum(tmpword) == 0) { addpair(tmpword); } memset(tmpword, 0, 100); ptmpword = tmpword; }
else if(isdelims(tmpch) == 0) { *ptmpword++ = tmpch; }
else if(isdelims(tmpch) == 2) { fgetc(); } }
sortpair();
if(wordlist.pairnum >= 10) { for( i = 0; i < 10; i++) { lowerprint( wordlist.pairs[i].word); } }
else { for( i = 0; i < wordlist.pairnum; i++) { lowerprint( wordlist.pairs[i].word); } }}