fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int main() {
  6. int size=0,i,n=20;
  7. char str[]="Ola. Tudo bem?\n Sim e contigo?\n Comigo esta tudo bem! Que tens feito?\n Trabalho no projeto!\n";
  8. char **matriz;
  9. int *linha;
  10. int *coluna;
  11. size = strlen(str);
  12. printf("%d\n",size);
  13.  
  14. int separadores = 0;
  15.  
  16. for (i=0; i < size; ++i){
  17. if (str[i] == '\n'){
  18. separadores++;
  19. }
  20. }
  21.  
  22. separadores++;
  23.  
  24. matriz = malloc(sizeof(char*)*separadores);
  25. int ultimo = 0, j=0;
  26.  
  27. for (i=0; i < size; ++i){
  28. if (str[i] == '\n' || i == (size-1)){
  29. matriz[j] = malloc(sizeof(char)*(i-ultimo));
  30. memcpy(matriz[j], str+ultimo, i-ultimo);
  31. matriz[j][i-ultimo]='\0';
  32. ultimo=i+1;
  33. j++;
  34. }
  35. }
  36.  
  37.  
  38. for(i=0; i < j;++i){
  39. printf("%s\n",matriz[i]);
  40. }
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
92
Ola. Tudo bem?
 Sim e contigo?
 Comigo esta tudo bem! Que tens feito?
 Trabalho no projeto!