For example:
Friendly.
I don't like the "ly" at the end of the word. Can I tokenize this string by "ly"
someCharVariable = strtok("friendly", "ly")?
For example:
Friendly.
I don't like the "ly" at the end of the word. Can I tokenize this string by "ly"
someCharVariable = strtok("friendly", "ly")?
The answer is no. Your example of "ly" will delimit on any occurrence of either "l" or "y" or "yl" or "ly"
The delimiter parameter is an array of characters, each meant to act as a delimiter.
This is an example of what you asked for:
char *iterate(char *p, const char *d, const size_t len)
{
while(p!=NULL && *p && memcmp(p, d, len)==0)
{
memset(p, 0x0, len);
p+=len;
}
return p;
}
char **
tokenize( char **result, char *working, const char *src, const char *delim)
{
int i=0;
char *p=NULL;
size_t len=strlen(delim);
strcpy(working, src);
p=working;
for(result[i]=NULL, p=iterate(p, delim, len); p!=NULL && *p; p=iterate(p, delim, len) )
{
result[i++]=p;
result[i]=NULL;
p=strstr(p, delim);
}
return result;
}
strtok returns char *. so u need to use somechar*var not somecharvariable.
your code will return the pointer to string "friend" and "l" will be replaced by '/0'.