Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

strtok function

Posted on 2004-09-06
4
1,005 Views
Last Modified: 2006-11-17
Hi to all experts.
I am trying to write my own strtok function and make it work exactly the same as the one in string.h.
At the moment, my code is as of below and it is not working perfectly.

Could someone help me?


#include <stdio.h>
#include <string.h>

char *my_strtok(const char *string, const char *delimiters);

int main()
{
      char string[]="A string\tof ,,tokens\nand some  more tokens";
      char seps[]=" ,\t\n";
      char *token;

      printf("%s\n\nTokens:\n", string);
      /* Establish string and get the first token: */
      token = my_strtok(string, seps);
      while( token != NULL )
      {
            /* While there are tokens in "string" */
            printf("%s\n", token);
            /* Get next token: */
            token = my_strtok(NULL, seps);
      }

      return 0;

}

/*
Sequentially truncate string if delimiter is found
*/
char *my_strtok(const char *string, const char *delimiters)
{
      int i, j;
      int tocontinue=1;
      char *ptr=NULL;
      static char *next=NULL;

      /* check for NULL pointers and empty strings */
      /* this is the 1st entry */
      if ( delimiters == NULL || delimiters[0] == '\0' )
            return NULL;
      else
      {
            if ( next == NULL && string == NULL )
                  return NULL;
            else
            {
                  /* at first */
                  if ( next == NULL )
                        next = (char *)string;
                  
                  for ( i=0; i<(int)strlen(next) && tocontinue; i++ )
                  {
                        for ( j=0; j<(int)strlen(delimiters); j++ )
                        {                              
                              /* delimiter match found */
                              if ( next[i] == delimiters[j] )
                              {
                                    tocontinue = 0;
                                    next[i] = '\0';
                                    ptr = next;
                                    
                                    /* last character in string met */
                                    if ( i == ((int)strlen(next) - 1) )
                                          next = NULL;
                                    /* move to the address after the found delimiter */
                                    else
                                          next = next + i + 1;
                                    
                                    break;
                              }
                              
                        }
                  }
                  
                  return ptr;
            }
      }
}
0
Comment
Question by:ee_guest
  • 2
  • 2
4 Comments
 
LVL 15

Expert Comment

by:efn
ID: 11991436
I noticed a few things.

The first parameter to my_strtok should not be declared const char *, because the function can change the characters addressed by the pointer.  It should be char *, matching the declaration of the standard strtok function.

The break statement gets control out of the j loop, but you want it to get out of the i loop too.  Depending on how you fix this, you may be able to get away with setting next to NULL inside a loop that evaluates strlen(next) as a terminating condition, but in general, this is not a good idea.

If string is not NULL, it should be assigned to next, whether next is NULL or not.  That is, the assignment should depend on the content of string, not the content of next.  As it is, assigning to next only if next is not NULL, the function can't work more than once.

--efn
0
 

Author Comment

by:ee_guest
ID: 11992872
>>The first parameter to my_strtok should not be declared const char *
But then that's what the strtok function in string.h is being declared as.

>> but you want it to get out of the i loop too
I used the tocontinue variable to break out the i loop.
0
 
LVL 15

Accepted Solution

by:
efn earned 100 total points
ID: 11993037
>>>>The first parameter to my_strtok should not be declared const char *
>>But then that's what the strtok function in string.h is being declared as.

Then you have a nonstandard string.h, as well as one that doesn't make sense.  The standard for C specifies char *, and strtok must modify the string in order to do its job.

>>>> but you want it to get out of the i loop too
>>I used the tocontinue variable to break out the i loop.

OK, I accept that, but you still have a problem in that it may try to evaluate strlen(next) when next is NULL.  That can't be helpful.

A couple more points:

The posted function only finds tokens terminated by characters in the delimiter string.  It should also find the last token delimited by end of the source string, even if it is not terminated by a delimiter.

It should skip past leading delimiters.  For example, if 'x' is a delimiter and the input is "xaxxb", it should return first "a", then "b".  It doesn't do that now.

--efn
0
 

Author Comment

by:ee_guest
ID: 11999046
thanks anyway..
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question