#include #include #include #include #define Wordlength 40 // just steal some btree stuff more or less straight from K&R struct bnode { char *word; int count; struct bnode *left; struct bnode *right; } struct bnode *bnodehere() { return (struct bnode *) malloc(sizeof(struct bnode)); } struct bnode *graft(struct bnode *n, char *w) { int cond; if (n == NULL) { n = bnodehere(); n->word = strdup(w); n->count = 1; n->left = n->right = NULL; } else if ((cond = strcmp(w, n->word)) == 0) { n->count++; } else if (cond < 0) { n->left = graft(n->left, w); } else { n->right = graft(n->right, w); } return n; } void bcat(struct bnode *n) { if (n != NULL) { bcat(n->left); printf("%6d %s\n", n->count, n->word); bcat(n->right); } } int nextword(char *teacup) { int tix = 0; char c; while(c = getchar()) { if (c == EOF) return(0); if (!isalpha(c)) continue; while (isalpha(c)) { teacup[tix++] = tolower(c); c = getchar(); } teacup[tix] = '\0'; return(1); } } char triple(char *new) { } int main() { int looplength = 3; char loop[looplength]; struct bnode *root; char word[Wordlength]; root = NULL; while (nextword(word)) { root = graft(root, word); } bcat(root); return(0); }