#include #include #include #include int readword (FILE * file, struct stralloc * sa) { int c; if (feof (file)) return 0; if (!stralloc_copys (sa, "")) return 0; while (1) { c = fgetc (file); if (c < 0) break; if (isspace (c)) { if (!sa->len) continue; else break; } if (!stralloc_readyplus (sa, 1)) return 0; sa->s[sa->len] = c; sa->len++; } return sa->len; } int main (int argc, char ** argv) { stralloc sa = {0}; stralloc dsts[1+'z'-'a']; FILE * file; int idx; memset (dsts, 0, sizeof (dsts)); switch (argc) { case 1: file = stdin; break; case 2: file = fopen (argv[1], "r"); if (!file) { perror ("fopen"); return 1; } break; default: fprintf (stderr, "Too many arguments\n"); return 1; } while (readword (file, &sa)) { if (!sa.len) continue; if ((sa.s[0] < 'a') || (sa.s[0] > 'z')) continue; idx = sa.s[0] - 'a'; if (dsts[idx].len) { if (!stralloc_append (dsts+idx, " ")) return 1; } if (!stralloc_cat (dsts+idx, &sa)) return 1; } for (idx = 0; idx <= 'z' - 'a'; ++idx) { if (!stralloc_0 (dsts+idx)) return 1; printf ("%s\n", dsts[idx].s); } return 0; }