Skip to content

Instantly share code, notes, and snippets.

@kopiro
Created March 25, 2013 14:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kopiro/5237558 to your computer and use it in GitHub Desktop.
Save kopiro/5237558 to your computer and use it in GitHub Desktop.
char** explode(char delimiter, char* str) {
int l = strlen(str), i=0, j=0, k=0;
char x = NULL;
char** r = (char**)realloc(r, sizeof(char**));
r[0] = (char*)malloc(l*sizeof(char));
while (i<l+1) {
x = str[i++];
if (x==delimiter || x=='\0') {
r[j][k] = '\0';
r[j] = (char*)realloc(r[j], k*sizeof(char));
k = 0;
r = (char**)realloc(r, (++j+1)*sizeof(char**));
r[j] = (char*)malloc(l*sizeof(char));
} else {
r[j][k++] = x;
}
}
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment