I am trying to use strtok()
. Following is the piece of code that I wrote. It does not work but prints ", '"
infinitely.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char str[]="this, by the way, is a 'sample'";
char *tokens;
tokens = strtok(str, ", '");
//printf("%s\n",tokens);
//printf("%s\n", str);
while(tokens!=NULL)
{
printf("%s\n", tokens);
tokens = (NULL, ", '");
}
return 0;
}
Following is the code from a strtok()
manual page, which works perfectly fine.
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
I feel I have done the exact same thing. Can't figure out the fault in my code. Could someone please point out.
,
comma operator's behavior no error generated While linked question is about the behavior of operator. – Burlington