user14675723
user14675723

Reputation:

strtok weird behavior on tokenization

The below code gives unexpected behaviour.

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

int main()
{
    
    char s[] = "hello $";
    char *t;
    t = strtok(s, "$$");
    printf("%s", t);

    return 0;
}

Why does this output hello and not hello $?

Upvotes: 0

Views: 66

Answers (2)

Steve Summit
Steve Summit

Reputation: 47942

In a comment you wrote:

so how should I split by a string?

There is no standard way (no predefined library function) for doing this, that I am aware of.

You could write your own split-on-string loop, using strstr. Here is one way:

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

int main()
{
    char s[] = "hello$$this$$is$$a$$test";
    char *sep = "$$";
    char *p = s;
    char *p2;
    int i = 1;
    do {
        p2 = strstr(p, sep);
        if(p2 != NULL) *p2 = '\0';
        printf("%d: %s\n", i++, p);
        if(p2 != NULL) p = p2 + strlen(sep);
    } while(p2 != NULL);
}

This could be improved, but it works, and it should get you started.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310970

From the C Standard (7.23.5.8 The strtok function)

2 A sequence of calls to the strtok function breaks the string pointed to by s1 into a sequence of tokens, each of which is delimited by a character from the string pointed to by s2

In this call

t = strtok(s, "$$");

the parameter s2 is set as having two identical delimiters. So the call is equivalent to

t = strtok(s, "$");

If you need to find the substring "$$" in a string you can use the standard C string function strstr. For example

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

int main(void) 
{
    char s[] = "hello $";
    
    char *p = strstr( s, "$$" );
    
    if ( p ) *p = '\0';
    
    puts( s );
    
    return 0;
}

Upvotes: 2

Related Questions