#include #include #include #include static char *sdup(char *s) { size_t n = strlen(s)+1; return memcpy(malloc(n), s, n); } static int add(char *s) { ENTRY *e; e = hsearch((ENTRY){.key = s}, ENTER); if (e->key == s) { /* only do the string copy if s is a new string */ e->key = sdup(s); return 1; } return 0; } static int getword(char *s) { int c; for (;;) { c = getchar(); if (c == EOF) return 0; if (!(c == ' ' || c == '\n' || c == '\r' || c == '\t')) break; } for (;;) { *s++ = c; c = getchar(); if (c == EOF || c == ' ' || c == '\n' || c == '\r' || c == '\t') break; } *s = 0; return 1; } int main(void) { char buf[4096]; int c = 0; /* start with a small table so resize is tested */ hcreate(64); while (getword(buf)) c += add(buf); // hdestroy(); printf("%d\n", c); return 0; }