How to write a C Program to Returns: array of decoded values. [0] - count of values in C Programming Language ?
Solution:
#include <stdlib.h>
#include <string.h>
/* Returns: array of decoded values. [0] - count of values */
int *decode(char *encoded) {
int idx = 1;
int maxlen = 8;
char *token;
char *delim = " :;,|";
int *nums = malloc(maxlen * sizeof(int));
/* Preserve the original string */
encoded = strdup(encoded);
token = strtok(encoded, delim);
nums[1] = atoi(token);
while (token = strtok(NULL, delim)) {
if (idx >= maxlen) {
maxlen += 8;
nums = realloc(nums, maxlen * sizeof(int));
}
nums[++idx] = atoi(token);
}
nums[0] = idx;
return nums;
}