• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

how is strtok written?

Hi,

I am trying to write the code as to how strtok is written (u know, something like an implementation of strcpy etc)... I am okay with what happens on the first call... something like this maybe:

char *strtok(const char *s1, char *delim)
{
    char *retStr;
    int i=0;

    while(s1[i]  != *delim) {i++;}

    retStr = (char *)malloc(i*sizeof(char));
    return strncpy(retStr, s1, i);
}

I am not so strong with pointers and therefore please correct me if I am wrong in the function. But what next? when I pass NULL on my second call, how does the function know my previous string? how does it send the second token?

Thanks
0
srinivas_vemla
Asked:
srinivas_vemla
2 Solutions
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi srinivas_vemla,

You're going to need a static pointer variable to keep track of the "current" position.  This becomes the variable that you'll increment as you advance through the string.

Then, when you enter your routine:

  if (s1!=NULL)
    TheStaticPointer = s1;

That should just about do it.



Good Luck!
Kent
0
 
Jaime OlivaresCommented:
0
 
srinivas_vemlaAuthor Commented:
Thanks guys,

The suggestion and the links were very helpful...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now