Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char * func_explode (char *str, char sep)
{
int i;
char * newstr;
printf("str: '%s' sep: '%c'\n", str, sep);
newstr = malloc(strlen(str) + 2);
if (newstr == NULL)
{
/* error */
return(NULL);
}
memset(newstr, 0, strlen(str) + 2);
strcpy(newstr, str);
for (i = 0; i < strlen(str); i++)
{
if (newstr[i] == sep) newstr[i] = '\0';
}
return(newstr);
}
int main (int argc, char **argv)
{
char *zin = "Hoi, ik ben bart enzo :D";
char sep = ' ';
char * nieuwzin;
char * ptr = NULL;
nieuwzin = func_explode(zin, sep);
ptr = nieuwzin;
if (ptr != NULL)
{
for ( ptr = nieuwzin;
*ptr != '\0';
ptr += strlen(ptr) + 1 )
{
printf("%s\n", ptr);
}
free(nieuwzin);
}
return(0);
}