Why does this code not work can strtok not be used for two variables simultaneously

#include<bits/stdc++.h>
using namespace std;
int main()
{
char s[100] = “All i want is burger, static, pluto and beer”;

char *ptr = strtok(s," ");
cout<<ptr<<endl; 
//ptr always points the first word before 
//the word all is then stored in an array like a,l,l,/0(null)

ptr =strtok(NULL," ");//this prints till i
cout<<ptr<<endl;

// so in order to put this words in order it should be in a loop

while(ptr!=NULL)//this starts from want
{
    ptr =strtok(NULL," ");
    cout<<ptr<<endl;
}

cout<<endl;

char *ptr1 =strtok(s,",");
 cout<<ptr1<<endl;

 while(ptr1!=NULL)
{
    ptr1 =strtok(NULL,",");
    cout<<ptr1<<endl;
}

}

You cannot use strtok twice.


https://cboard.cprogramming.com/c-programming/132953-strtok-twice-means-problems.html

Refer these for more details.

If you pass both space and comma as delimeter together…then it will split the string about both comma and space.
So remove the 2nd strtok function and change the 6th line to char * ptr = strtok(s," ,"); and then check.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.