Advertisement
KDOXG

Ex. 8

Oct 26th, 2018
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /**Algumas observações:
  6.  * Para dividir a string em substrings, passe como referência o elemento após o espaço (linhas 15, 17, 35 e 37);
  7.  * Se a aritmética de ponteiros for em base dos índices da string, como no meu caso, é importante lembrar que precisa retornar a referência toda hora para o começo (linhas 42 e 47).
  8.  */
  9.  
  10. int* identifica(char linha[])
  11. {
  12.     int i, j=1, *aux=malloc(((strlen(linha)/2)+1)*sizeof(int));
  13.     for (i=0; i<strlen(linha); i++)
  14.     {
  15.         if (linha[i-1] == ' ')
  16.         {
  17.             aux[j] = i;
  18.             j++;
  19.         }
  20.     }
  21.     aux[0]=j;
  22.     return aux;
  23. }
  24.  
  25. char** separar(char linha[])
  26. {
  27.     int i, j, k=1, *aux=identifica(linha), aux2=strlen(linha);
  28.     int l=aux[0];
  29.     char **vetor=malloc(l*sizeof(char*)), *aux3;
  30.     for (i=0; i<l; i++)
  31.         vetor[i]=malloc(80*sizeof(char));
  32.    
  33.     for (i=0; i<aux2; i++)
  34.     {
  35.         if (i == aux[k])
  36.         {
  37.             linha[i-1] = '\0';
  38.             k++;
  39.         }
  40.     }
  41.     k=0;
  42.     aux3=linha;
  43.    
  44.     for (j=l-1; j>=0; j--)
  45.     {
  46.         strcpy(*(vetor+k),linha);
  47.         linha=aux3;
  48.         linha=linha+aux[k+1];
  49.         k++;
  50.     }
  51.     return vetor;
  52. }
  53.  
  54. void main() {
  55.     char linha[80], **vetor, aux[80];
  56.     fgets(linha,80,stdin);
  57.     strcpy(aux,linha);
  58.     vetor=separar(linha);
  59.     int i, *l=identifica(aux);
  60.     for (i=0; i<*l; i++)
  61.         printf("%s\n", vetor[i]);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement