strtok problem

R

rayw

To filter the 'x' out of this, can I use strtok in a for loop like
below please?

I get an error when I run this, however, I cannot fathom out why.

char c[] = "blahxblah";

char * p;

for(p = "x"; p = strtok(c, p); p = NULL)
{
printf("%s", p);
}
 
Z

Zero

Moreover, x is used to separate blah from blah,
with strtok, you would not get the second blah :)
 
T

Thad Smith

rayw said:
To filter the 'x' out of this, can I use strtok in a for loop like
below please?

When posting code, try to post a complete program that illustrates your
problem. The code you sent doesn't show file inclusions or a function
definition.

Your code, as written, doesn't work, but yes, you can use strtok in a
loop to extract (and print) string segments not containing 'x'.
I get an error when I run this, however, I cannot fathom out why.

char c[] = "blahxblah";

char * p;

for(p = "x"; p = strtok(c, p); p = NULL)
{
printf("%s", p);
}

Check the documentation for strtok. You call it with incorrect
parameters the second and succeeding times.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Access violation reading location 0
Why does strcat mess up the tokens in strtok (and strtok_r)? 92
strtok problem 16
Infinite loop problem 1
strtok() 13
strtok 7
PyObject_CallObject freezing 0
strtok problem 5

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,092
Latest member
vinaykumarnevatia1

Latest Threads

Top