Due to the uncertainty of the words, binary trees are used for implementation:
// Treenode. htypedef struct _ treenode {int count; // number of occurrences char * word; // The struct _ treenode * left; struct _ treenode * right;} treenode; // allocate memory to treenode * talloc (void) {return (treenode *) malloc (sizeof (treenode);} // print tree void tprint (treenode * root) {// print left-> Self-> right IF (root! = NULL) {tprint (root-> left); printf ("% 4D % s \ n", root-> count, root-> word ); tprint (root-> right) ;}} // Add the word to the appropriate node location treenode * addnode (treenode * node, const char * Word) {int con; treenode * TMP; if (node = NULL) {node = talloc (); node-> COUNT = 1; node-> word = strdup (Word ); node-> left = node-> right = NULL;} else if (con = strcmp (word, node-> word) <0) {TMP = addnode (node-> left, word); node-> left = TMP;} else if (con> 0) {TM P = addnode (node-> right, word); node-> right = TMP;} else {node-> count ++;} return node ;} /** read the word from the specified stream */INT getword (char * Ch, size_t N, file * f) {int C; char * P = CH; while (isspace (C = fgetc (F); If (C! = EOF) * P ++ = C; If (! Isalpha (c) {* P = '\ 0'; return C ;}for (; -- N> 0; P ++) {If (! Isalnum (* P = fgetc (F) {ungetc (* P, f); break ;}* P = '\ 0'; return C ;} // whether the memory occupied by the tree is void treefree (treenode * root) {If (root! = NULL) {treefree (root-> left); free (root-> word); // release the node's Word Memory free (Root ); // whether the memory occupied by the node is treefree (root-> right) ;}// test. C # include <stdio. h> # include <stdlib. h> # include "treenode. H "# define Max 100int main (INT argc, char * argv []) {file * F; char W [Max] = {0}; char * fname =" treenode. H "; if (F = fopen (fname," R "))! = NULL) {treenode * root = NULL; while (getword (W, Max, f )! = EOF) {If (isalpha (W [0]) root = addnode (root, W);} tprint (Root); treefree (Root ); fclose (f);} else {printf ("Open % s error \ n", fname);} getchar (); Return 0 ;}