Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main ()
  5. {
  6.   char str[] = "strtok needs to be called several times to split a string";
  7.   char *delim = " ";
  8.   char *tmp_part_ptr;
  9.  
  10.   char **parts = (char**)malloc(100*sizeof(char*));
  11.  
  12.   int index = 0;
  13.  
  14.   tmp_part_ptr = strtok(str, delim);
  15.   while (tmp_part_ptr != NULL) {
  16.     // printf("'%s'\n", tmp_part_ptr);
  17.  
  18.     parts[index] = (char*) malloc(100*sizeof(char));
  19.     strcpy(parts[index], tmp_part_ptr);
  20.  
  21.     // printf("'%s'\n", parts[index]);
  22.  
  23.     index++;
  24.  
  25.         tmp_part_ptr = strtok(NULL, delim);
  26.   }
  27.  
  28.   parts[index] = "/0";
  29.  
  30.   int parts_arr_length = index;
  31.  
  32.   for (int j = 0; j < parts_arr_length; j++) {
  33.     printf("'%s' \n", *(parts + sizeof(char) * j));
  34.   }
  35.  
  36.   return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement