fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char **read_command(char *text) {
  6. int index=0;
  7. char **res=NULL;
  8. char *command= malloc(strlen(text)+1);
  9. strcpy(command, text);
  10. char *tok = strtok(command, " ");
  11. while(tok!=NULL) {
  12. res = realloc(res, sizeof(char*)*(index+1));
  13. char *dup = malloc(strlen(tok)+1);
  14. strcpy(dup, tok);
  15. res[index++] = dup;
  16. tok = strtok(NULL, " ");
  17. }
  18. res[index++]=NULL;
  19. free(command);
  20. return res;
  21. }
  22.  
  23. int main(void) {
  24. char *input="read A B C";
  25. char **command = read_command(input);
  26. for (int i = 0 ; command[i] ; i++) {
  27. printf("'%s'\n", command[i]);
  28. }
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
'read'
'A'
'B'
'C'