C program for splitting a string
C Program to demonstrate how to split a string using strtok().
// C program for splitting a string
// using strtok()
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Geeks-for-Geeks-is-a-computer-science-portal";
// Returns first token
char* token = strtok(str, " - ");
// Keep printing tokens while one of the
// delimiters present in str[].
while (token != NULL) {
printf(" % s\n", token);
token = strtok(NULL, " - ");
}
return 0;
}
Output
Geeks for Geeks is a computer science portal
Time complexity : O(n)
Auxiliary Space: O(n), where n is the number of characters in the string.
How to Split a String by a Delimiter in C?
Splitting a string by a delimiter is a common task in programming. In C, strings are arrays of characters, and there are several ways to split them based on a delimiter. In this article, we will explore different methods to split a string by a delimiter in C.