Start New Discussion within our Software Development Community

So I'm trying to remove 2 brackets from lines I'm reading from a file.
For some reason when I run my program the characters in between the delimeters is removed.

for example
greetings pe()ple
would end up as
greetings pe

I'm using a char array of delimiters.
char delimiters[] = " ( )";

Why does strtok delete the characters between the parenthesis?

Remember that strtok (which frankly I would avoid using if possible) works by overwriting the delimiters with '\0', it is not deleting anything. It returns pointers into the original string.

So when you call

char *token;
token = strtok("greetings pe()ple", "()");

it scans forward until it finds the '(', replaces it with '\0' and returns a pointer to the 'g' giving a string of "greetings pe". If you then call strtok again it

token = strtok(NULL, "()");

It scans past the ')' to the end of the string, finding no more delimiters it returns a pointer to "ple". At the end of this your original string now contains

"greetings pe\0)ple"

Nothing has been deleted but the '(' has been overwritten.

This question has already been answered. Start a new discussion instead.