You need to use the pointer that strtok() returns for your comparison instead of the original string. Here:
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[100];
char *s;
puts("Wrong method:");
strcpy(string, "foo\nbar\nbaz\n");
s = strtok(string, "\n");
while(s)
{
puts(string);
s = strtok(NULL, "\n");
}
puts("\nRight method:");
strcpy(string, "foo\nbar\nbaz\n");
s = strtok(string, "\n");
while(s)
{
puts(s);
s = strtok(NULL, "\n");
}
return 0;
}
Code:
Wrong method:
foo
foo
foo
Right method:
foo
bar
baz