Solved

strtok function

Posted on 2004-09-06
4
1,004 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to declare a 2-dimensional version of strlen 4 206
My eclipse editor won't start ? 4 154
Unable to start eclipse ? 17 141
C++ :Change value from  DisableCMD registry 4 59
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now