Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. void parser(char* toparse, char separator, char*** list);
  6.  
  7. int main()
  8. {
  9.         int i;
  10.         char* toparse = "me2000,calypso,null";
  11.         char** list;
  12.         parser(toparse, ',', &list);
  13.         for (i=0; list[i] != NULL; i++)
  14.         {
  15.                 printf("Word : %s\n", list[i]);
  16.         }
  17.         return 0;
  18. }
  19.  
  20. void parser(char* toparse, char separator, char*** list)
  21. {
  22. int i=0, j=0, nbwords=0, currentword=0;
  23.  
  24.         for (i=0; toparse[i] != '\0'; i++)
  25.         {
  26.                 if (toparse[i] == separator)
  27.                         nbwords++;
  28.         }
  29.  
  30.         printf("nbwords : %d\n", nbwords);
  31.  
  32.         (*list) = malloc((nbwords+2)*sizeof(char*));
  33.  
  34.         for (i=0; toparse[i] != '\0'; i++)
  35.         {
  36.                 printf("char : %c\n",toparse[i]);
  37.                 if (toparse[i] == separator)
  38.                 {
  39.                         printf("currentword : %d, j : %d\n", currentword, j);
  40.                         *list[currentword] = malloc(j+1);
  41.                         memset(*list[currentword], 0, j+1);
  42.                         memcpy(*list[currentword], &toparse[i-j], j);
  43.                         printf("word : %s\n", *list[currentword]);
  44.                         currentword++;
  45.                         j=0;
  46.                 }
  47.                 else
  48.                 {
  49.                         j++;
  50.                 }
  51.                 *list[currentword+1] = NULL;
  52.         }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement