CodexBloom - Programming Q&A Platform

advanced patterns When Using `strtok` with Multi-Delimiters in C

👀 Views: 1125 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-11
c strtok tokenization C

I'm stuck on something that should probably be simple. I've been banging my head against this for hours. I'm working with an scenario with the `strtok` function in C. I am trying to tokenize a string that has multiple delimiters, but the output is inconsistent, and I believe I'm not using it correctly. Here's the part of my code that handles the tokenization: ```c #include <stdio.h> #include <string.h> int main() { char str[] = "Hello, world; this is a test!" char *token; const char delimiters[] = ",;! "; // Multiple delimiters token = strtok(str, delimiters); while (token != NULL) { printf("%s\n", token); token = strtok(NULL, delimiters); } return 0; } ``` I expect this to print each word separately, but it seems to skip over some tokens. For example, it skips 'world;' and only returns 'this' instead of 'this is'. I've verified that there are no extra spaces, and I checked for null terminators. I've also tried to adjust the delimiter string to see if it was the question, but the output remains the same. I'm compiling with GCC version 10.2. Should I be using a different approach for handling multiple delimiters, or is there a specific way I need to structure my string? Any insights would be appreciated! My development environment is Linux. Is there a better approach?