fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct vector_t {
  6. void **v;
  7. int length;
  8. int size;
  9. } vector;
  10.  
  11. vector new_v() {
  12. vector a;
  13. a.length = 0;
  14. a.size = 8;
  15. a.v = (void **)malloc(sizeof(void *) * a.size);
  16. return a;
  17. }
  18.  
  19. void push_v(vector *a, void *e) {
  20. if(a->length == a->size) {
  21. a->size *= 2;
  22. a->v = (void **)realloc(a->v, sizeof(void *) * a->size);
  23. }
  24. a->v[a->length] = e;
  25. a->length++;
  26. }
  27.  
  28. void *pop_v(vector *a) {
  29. a->length--;
  30. return a->v[a->length];
  31. }
  32.  
  33. void free_v(vector a) {
  34. free(a.v);
  35. }
  36.  
  37. void map_v(vector a, void (*f)(void *)) {
  38. int i;
  39. for(i = 0; i < a.length; i++)
  40. f(a.v[i]);
  41. }
  42.  
  43. void printer(void *a) {
  44. printf("%s ", (char *) a);
  45. }
  46.  
  47. int main(void) {
  48. vector v = new_v();
  49. char str[100];
  50. char *token, *tmp;
  51. fgets(str, 100, stdin);
  52. token = strtok(str, " \t\n?!,.;:-_");
  53. while(token != NULL) {
  54. tmp = (char *)malloc(sizeof(char) * (strlen(token) + 1));
  55. strcpy(tmp, token);
  56. push_v(&v, tmp);
  57. token = strtok(NULL, " \t\n?!,.;:-_");
  58. }
  59. map_v(v, printer);
  60. map_v(v, free);
  61. free_v(v);
  62. return 0;
  63. }
  64.  
Success #stdin #stdout 0s 2384KB
stdin
Ciao, come va oggi?!
stdout
Ciao come va oggi