![]() |
VOOZH | about |
In C, strings are arrays of characters using string manipulation often requires splitting a string into substrings based on multiple delimiters. In this article, we will learn how to split a string by multiple delimiters in C.
Example
Input:
char inputString[] = "Hello,World;This|is.GeeksForGeeks";
char delimiters[] = ",;.|" ;
Output:
Hello
World
This
is
GeeksForGeeks
To split a by multiple delimiters in C, we can use thefunction from the standard library. This function allows us to specify multiple delimiters and iteratively extract tokens from the string by calling this function in a loop to get all tokens and when there are no more tokens it returns NULL otherwise, returns the pointer to the first token encountered in the string.
char *strtok(char *str, const char *delims);
Here,
The following example demonstrates how to split a given string by multiple delimiters in C.
Hello World This is GeeksForGeeks
Time complexity: O(n)
Auxiliary Space: O(1), as strtok() modifies the input string in place and does not require additional space for tokens