#include /* for getchar, ungetc, stdin, EOF */ #include /* for malloc, realloc, free, size_t */ #include /* for isspace */ int main(int argc, char **argv) { size_t n; char *p; for (p = malloc(n = 1);; p = realloc(p, ++n)) { int c; if (p == NULL) { fprintf(stderr, "%s: out of store\n", argv[0]); return EXIT_FAILURE; } if ((c = getchar()) == EOF) { break; } if (isspace((unsigned char)c)) { ungetc(c, stdin); break; } p[n - 1] = c; } p[n - 1] = '\0'; printf("The word \"%s\" was terminated by the ", p); if (feof(stdin)) { printf("end of file.\n"); } else { printf("whitespace character '\\x%02x'.\n", (unsigned char)getchar()); } free(p); return feof(stdin) && !ferror(stdin) ? EXIT_SUCCESS : EXIT_FAILURE; }