Created
March 25, 2013 14:37
-
-
Save kopiro/5237558 to your computer and use it in GitHub Desktop.
explode function in C (http://www.php.net/manual/en/function.explode.php)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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