Advertisement
mtrower

decode.c

Jan 15th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3.  
  4. /* Returns: array of decoded values.  [0] - count of values */
  5. int *decode(char *encoded) {
  6.     int idx = 1;
  7.     int maxlen = 8;
  8.     char *token;
  9.     char *delim = " :;,|";
  10.     int *nums = malloc(maxlen * sizeof(int));
  11.  
  12.     /* Preserve the original string */
  13.     encoded = strdup(encoded);
  14.  
  15.     token = strtok(encoded, delim);
  16.     nums[1] = atoi(token);
  17.  
  18.     while (token = strtok(NULL, delim)) {
  19.         if (idx >= maxlen) {
  20.             maxlen += 8;
  21.             nums = realloc(nums, maxlen * sizeof(int));
  22.         }
  23.         nums[++idx] = atoi(token);
  24.     }
  25.  
  26.     nums[0] = idx;
  27.     return nums;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement