fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char** split(char *str, int *qtd){
  6. *qtd = 0;
  7. char **strings = NULL;
  8. int i = 0;
  9. char *pch = strtok (str," ");
  10. while (pch != NULL){
  11. strings = realloc(strings, sizeof(char*) * (*qtd + 1));
  12. strings[i] = malloc(strlen(pch) + 1);
  13. strcpy(strings[i++], pch);
  14. pch = strtok (NULL, " ");
  15. }
  16.  
  17. return strings;
  18. }
  19.  
  20. int main () {
  21. char str[] = "Bom dia pessoal";
  22.  
  23. int qtd_strings, i;
  24. char **strings = split(str, &qtd_strings);
  25. for (i = 0;i < qtd_strings;++i){
  26. printf("%s\n", strings[i]);
  27. }
  28.  
  29. //mostrar cada uma
  30. for (i = 0;i < qtd_strings;++i){
  31. printf("%s\n", strings[i]);
  32. }
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
Standard output is empty