How do I split a string by a multi-character delimiter in C?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C, splitting a string by a multi-character delimiter requires manual scanning because standard tokenizers like strtok are character-set based, not substring based. If delimiter is "||" or "<->", strtok cannot treat it as one atomic separator. A reliable solution uses strstr to locate delimiter occurrences and then slices segments carefully.
Core Sections
1) Why strtok is insufficient
strtok(s, "||") means split on any '|' character, not on the exact substring "||".
For exact multi-char separators, use substring search.
2) strstr-based splitter example
Usage:
3) Returning allocated tokens
For reusable libraries, build dynamic array of allocated strings and return count.
Define ownership contract explicitly to prevent leaks.
4) Edge-case behavior
Decide upfront:
- consecutive delimiters produce empty tokens?
- leading/trailing delimiters produce empty tokens?
- delimiter empty string is invalid?
Document these rules because parsing behavior depends on business requirements.
Validation and Production Readiness
After implementing any fix or pattern from this topic, validate behavior using a repeatable workflow rather than ad hoc spot checks. The most reliable process has three stages: reproduce baseline behavior, apply one focused change, then verify both expected and adjacent scenarios. This avoids false confidence from a single green run and helps isolate which change actually solved the problem.
A practical command-driven template:
If your project includes automated tests, convert the original failure into a regression test immediately. This is the fastest way to prevent the same issue from reappearing during later refactors, dependency upgrades, or environment changes.
Also validate edge cases explicitly. Many production defects occur not on the nominal path, but on boundary inputs such as empty collections, null/none values, unusual encodings, or large payloads. Define a compact table of edge scenarios and expected outcomes so reviewers can reproduce your checks quickly.
Before rollout, confirm environment parity. A fix that works in local development can fail in staging or production when runtime versions, OS behavior, file systems, networking, or resource limits differ. Capture version metadata and infrastructure assumptions in your PR or runbook.
Finally, define rollback criteria before deployment. If metrics or logs indicate regressions, teams should know exactly which change to revert and what signals trigger that decision. This operational discipline turns one-off troubleshooting into a maintainable engineering practice and significantly reduces incident recovery time.
Common Pitfalls
- Using
strtokand expecting multi-character delimiter semantics. - Forgetting to null-terminate copied token buffers.
- Leaking allocated memory in tokenization utilities.
- Failing to define behavior for empty tokens and edge delimiters.
- Passing empty delimiter and triggering undefined parsing logic.
Summary
To split by multi-character delimiter in C, use strstr-based scanning and explicit substring extraction. This gives precise separator semantics and full control over edge cases and memory ownership. With clear contracts and tests, custom splitters are reliable for parser and protocol workloads.
Related reading
- How do I start a process from C?
- How do I stop a thread when my winform application closes
- How do I submit disabled input in ASP.NET MVC?
- How do I sync the SVN revision number with my ASP.NET web site?
- How do I test an async method with NUnit or possibly with another framework?
- How do I trap CtrlC SIGINT in a C console app?
- How do I truncate a .NET string?
- How do I turn a C# object into a JSON string in .NET?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.