Solved

strtok function

Posted on 2004-09-06
4
1,000 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
Comment Utility
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
Comment Utility
>>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
Comment Utility
>>>>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
Comment Utility
thanks anyway..
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-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.

743 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

18 Experts available now in Live!

Get 1:1 Help Now