-2

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")?

ShadyBears
  • 3,955
  • 13
  • 44
  • 66
  • 2
    I'd be _lying_ if I said you could. – ldav1s Feb 14 '13 at 03:20
  • Ok then is there a way to tokenize a string by a string? – ShadyBears Feb 14 '13 at 03:21
  • There is. See `strstr`. But I was trying to point out that "ly" can appear elsewhere, and some more processing may be needed. – ldav1s Feb 14 '13 at 03:24
  • The only problem is with strstr points to the first occurence. Say I have a word such as xerox and want to take out x and replace it with "zz" (this is my hmwk assignment). I've been stuck on this part for awhile. Is there a way I can get strstr to point to more than one occurence? – ShadyBears Feb 14 '13 at 03:28
  • Why don't you just code up a 5 line demo program, and try it? You don't need to ask all of us. – abelenky Feb 14 '13 at 03:31
  • Yes. But you know the length of the delimiter so you can index past it to get to the rest of the word. – ldav1s Feb 14 '13 at 03:33

2 Answers2

4

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;
}
jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
0

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'.

gn.sendhil
  • 30
  • 2