Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- void parser(char* toparse, char separator, char*** list);
- int main()
- {
- int i;
- char* toparse = "me2000,calypso,null";
- char** list;
- parser(toparse, ',', &list);
- for (i=0; list[i] != NULL; i++)
- {
- printf("Word : %s\n", list[i]);
- }
- return 0;
- }
- void parser(char* toparse, char separator, char*** list)
- {
- int i=0, j=0, nbwords=0, currentword=0;
- for (i=0; toparse[i] != '\0'; i++)
- {
- if (toparse[i] == separator)
- nbwords++;
- }
- printf("nbwords : %d\n", nbwords);
- (*list) = malloc((nbwords+2)*sizeof(char*));
- for (i=0; toparse[i] != '\0'; i++)
- {
- printf("char : %c\n",toparse[i]);
- if (toparse[i] == separator)
- {
- printf("currentword : %d, j : %d\n", currentword, j);
- *list[currentword] = malloc(j+1);
- memset(*list[currentword], 0, j+1);
- memcpy(*list[currentword], &toparse[i-j], j);
- printf("word : %s\n", *list[currentword]);
- currentword++;
- j=0;
- }
- else
- {
- j++;
- }
- *list[currentword+1] = NULL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement