I have developed my own version of strtok. Just to practice the use of pointers.
Can anyone see any limitations with this or anyway I can improve.
void stvstrtok(const char *source, char *dest, const char token)
{
/* Search for the token. */
int i = 0;
while(*source)
{
*dest++ = *source++;
if(*source == token)
{
source++;
}
}
*dest++ = '\0';
}
int main(void)
{
char *long_name = "dog,sat ,on ,the,rug,in ,front,of,the,fire";
char buffer[sizeof(long_name)/sizeof(*long_name)];
stvstrtok(long_name, buffer, ',');
printf("buffer: %s\n", buffer);
getchar();
return 0;
}
A side note: The word 'token' is usually used to describe the parts of the string that are returned. Delimiter is used to describe the thing that separates the tokens. So to make your code more clear you should rename token to delimiter and rename dest to token_dest.
Differences in your function and strtok:
There are several differences between your function and strtok.
That being said, I will give suggestions for how to make a better function, not a function that is closer to strtok's implementation.
How to improve your function (not emulate strtok):
I think it would be better to make the following changes: