I have written the following function in C to try to tokenize a string. The function takes in a string to be tokenized (char * string), as well as a string of delimiting characters used to separate tokens from one another (char * delimiters).
char ** tokenize(char * string, char * delimiters)
{
int num_of_tokens = 0;
int itr = 0;
char ** tokens = NULL;
while (string[itr] != '\0')
{
if (!isDelimiter(string[itr], delimiters))
{
num_of_tokens++; /*if char is not a delimiter, we have found a new token*/
int temp_token_count = num_of_tokens - 1;
tokens = realloc(tokens, num_of_tokens);
tokens[temp_token_count] = malloc(STRING_SIZE * sizeof(char));
while(!isDelimiter(string[itr], delimiters) && string[itr] != '\0')
{
appendChar(tokens[temp_token_count], string[itr]);
itr++;
}
}
itr++;
}
return tokens;
}
From the main function, the call to the tokenize function looks like this:
int main()
{
char * string = "This would,,,,be";
char * delim = ",.:;*& ";
char ** tokens = tokenize(string, delim);
int x = 0;
while(x<3)
{
printf("%s\n", tokens[x]);
x++;
}
return 0;
}
I would expect the output from this call to produce:
This
would
be
However, this is what is being output:
L@?
would
be
This seems especially odd considering if I call the tokenize function with "This," as the input string, I receive back exactly what I would expect too:
This
I can't figure out whats going on and any help would be greatly appreciated, thanks for your time!!
Edit: This is the isDelimiter function
int isDelimiter(char test_char, char * delimiters)
{
int itr = 0;
while (delimiters[itr] != '\0')
{
if (test_char == delimiters[itr]) return 1;
itr++;
}
return 0;
}
tokens = realloc(tokens, num_of_tokens);should betokens = realloc(tokens, num_of_tokens*sizeof(char*));