fork download
  1. #define _GNU_SOURCE
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. char *word[26][100];
  8. int nword[26];
  9.  
  10. int main()
  11. {
  12. char *line = NULL;
  13. size_t len = 0;
  14. int i, j;
  15.  
  16. while (getline(&line, &len, stdin) != -1) {
  17. int index = -1;
  18. int c = line[0];
  19.  
  20. if ('a' <= c && c <= 'z') index = c - 'a';
  21. if ('A' <= c && c <= 'Z') index = c - 'A';
  22.  
  23. if (index >= 0 && nword[index] < 100) {
  24. strtok(line, "\n");
  25. word[index][nword[index]++] = strdup(line);
  26. }
  27. }
  28.  
  29. free(line);
  30.  
  31. for (i = 0; i < 26; i++) {
  32. printf("%c:", 'A' + i);
  33.  
  34. for (j = 0; j < nword[i]; j++) {
  35. printf(" %s", word[i][j]);
  36. free(word[i][j]);
  37. }
  38. printf("\n");
  39. }
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0s 2300KB
stdin
apple
apricot
cantaloupe
cherry
kiwi
mango
orange
papaya
peach
pear
strawberry
stdout
A: apple apricot
B:
C: cantaloupe cherry
D:
E:
F:
G:
H:
I:
J:
K: kiwi
L:
M: mango
N:
O: orange
P: papaya peach pear
Q:
R:
S: strawberry
T:
U:
V:
W:
X:
Y:
Z: