strTok function (thread safe, supports empty tokens, doesn't change string) I'm new to C language and want to explode a string like we do in PHP explode() function, I searched for a built-in function with the C standard library, and I found strtok , but It doesn't support empty tokens like 1,2,3,,5 . Inspired by the answers I found in this SO question I made this function, it is supposed to be thread safe and support empty tokens and doesn't change the original string

char* strTok(char** newString, char* delimiter)
{
    char* string = *newString;
    char* delimiterFound = (char*) 0;
    int tokLenght = 0;
    char* tok = (char*) 0;

    if(!string) return (char*) 0;

    delimiterFound = strstr(string, delimiter);

    if(delimiterFound){
        tokLenght = delimiterFound-string;
    }else{
        tokLenght = strlen(string);
    }

    tok = malloc(tokLenght + 1);
    memcpy(tok, string, tokLenght);
    tok[tokLenght] = '\0';

    *newString = delimiterFound ? delimiterFound + strlen(delimiter) : (char*)0;

    return tok;
}


I designed it to be used like

char* input = "1,2,3,4,5,6,7,,,10,";
char** inputP = &amp;input;
char* tok;
while( (tok=strTok(inputP, ",")) ){
    printf("%s\n", tok);
}
delimiterFound + strlen(delimiter) sounds like a bug. If the delimiter is longer than one character, *newString will point too far into the original, maybe even beyond the end. Correct me if I am wrong, delimiterFound + 1 is what you are actually after.
Modern C allows, and strongly encourages, to declare variables as close to their use a possible. Consider

char * delimiterFound = strstr(string, delimiter);
....
char * tok = malloc(tokLenght + 1);


etc.
Always test that malloc didn't fail.
More spaces - around keywords, braces, etc - definitely improve readability:

    if (....) {
        ....
    } else {
        ....
    }