Tehreem Tehreem - 1 year ago 152
C Question

Why is strtok printing only first word?

I am using strtok for converting string into individual words. I have done the following:

int main() {
char target[100];
char *t;
t = strtok(target," ");
while (t!= NULL)
printf("<<%s>>\n", t);
t = strtok (NULL, " ");
return 0;

The input is a string such as 'this is a string', the output I am getting is

Answer Source

The way you have written scanf it will accept string till white space only


SO You need to change the way you take input from console

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download