SHARE
TWEET

Untitled

a guest Nov 8th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. char** splitStr(char* orig_str, char* sep, int* size)
  6. {
  7.     char *token, *tofree, *str;
  8.     char** arr = calloc(1, sizeof(char*));
  9.     tofree = str = strdup(orig_str);
  10.     int i = 0;
  11.     while ((token = strsep(&str, sep))) {
  12.         arr[i] = calloc(strlen(token) + 1, sizeof(char));
  13.         strcpy(arr[i], token);
  14.         i++;
  15.         arr = reallocarray(arr, i + 1, sizeof(char*));
  16.     }
  17.     free(tofree);
  18.     *size = i;
  19.     return arr;
  20. }
  21.  
  22. void freeArr(char** arr, int size) {
  23.     for (int i = 0; i < size; i++) {
  24.         free(arr[i]);
  25.     }
  26.     free(arr);
  27. }
  28.  
  29. int main()
  30. {
  31.     int size;
  32.     char* str = "123812,ANA MARIA DEL CARPIO,1386.2,1239.9";
  33.     char** arr = splitStr(str, ",", &size);
  34.     for (int i = 0; i < size; i++) {
  35.         puts(arr[i]);
  36.     }
  37.     freeArr(arr, size);
  38.     return 0;
  39. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top