help implementing strtok()
hello there!
I wanted to write my own implementation of strtok(), which I call my_strtok().. here is my code:
Code:
char *token;
char* my_strtok(char *str, const char *delim)
{
int i;
size_t j;
char *string;
if (str!=NULL) {
string=str;
}
else
{
string=token;
}
if (my_strlen(string)==0)
return NULL;
int end=0;
i=0;
while (!end)
{
if (string[i]=='\0')
{
token=&string[i];
return NULL;
}
for (j=0;j<my_strlen(delim);j++)
{
if (string[i]==delim[j])
{
i++;
end=0;
break;
}
else
{
end=1;
}
}
}
char *tok;
tok=&string[i];
i++;
end=0;
while (!end)
{
if (string[i]=='\0')
{
token=&string[i];
return tok;
}
for (j=0;j<my_strlen(delim);j++)
{
if (string[i]==delim[j])
{
end=1;
break;
}
}
i++;
}
string[i-1]='\0';
token=&string[i];
return tok;
}
I found out that
causes a segmentation fault... Maybe at the moment I am mentally tired due to a long exercise session, but I cannot find an explaination at a glance... :confused:
What do you think in general of this implementation?
thanks a lot! bye