Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- int main ()
- {
- char str[] = "strtok needs to be called several times to split a string";
- char *delim = " ";
- char *tmp_part_ptr;
- char **parts = (char**)malloc(100*sizeof(char*));
- int index = 0;
- tmp_part_ptr = strtok(str, delim);
- while (tmp_part_ptr != NULL) {
- // printf("'%s'\n", tmp_part_ptr);
- parts[index] = (char*) malloc(100*sizeof(char));
- strcpy(parts[index], tmp_part_ptr);
- // printf("'%s'\n", parts[index]);
- index++;
- tmp_part_ptr = strtok(NULL, delim);
- }
- parts[index] = "/0";
- int parts_arr_length = index;
- for (int j = 0; j < parts_arr_length; j++) {
- printf("'%s' \n", *(parts + sizeof(char) * j));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement