-
-
Save lastguest/5238009 to your computer and use it in GitHub Desktop.
This file contains 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; | |
} | |
char** explode2(const char delimiter, const char* str) { | |
char *p = strcpy((char*)malloc(strlen(str)),str); | |
unsigned int cp_size = sizeof(char*); | |
char **r = (char**)malloc(cp_size); | |
unsigned long j = 1; | |
r[0] = p; | |
while(*p){ | |
if(*p == delimiter){ | |
r = (char**)realloc(r,(++j)*cp_size); | |
r[j-1] = p+1; | |
*p = 0; | |
} | |
p++; | |
} | |
return r; | |
} | |
int main(int argc, char *argv[]) { | |
char* test = "hello.friend.no.hard.feelings"; | |
char** parole = explode2('.',test); | |
int i = 0; | |
while(parole[i]){ | |
printf("%d: %s \n",i,parole[i++]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment