Split a String in C
In C, you can split a string into tokens (substrings) using functions like strtok()
from the standard library or by manually iterating through the string and extracting substrings based on delimiters.
In this tutorial, we will cover multiple ways to split a string in C with different approaches and examples.
Examples of Splitting a String in C
1. Splitting a String Using strtok()
In this example, we will use the strtok()
function to split a string into words based on spaces.
main.c
</>
Copy
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "apple banana cherry";
char *token = strtok(str, " ");
// Loop through the string and print each token
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, " ");
}
return 0;
}
Explanation:
- We declare a character array
str[]
containing the string"C programming is fun"
. - The function
strtok()
is used to split the string at spaces (" "
). strtok(str, " ")
returns the first token (word before the first space).- In a loop,
strtok(NULL, " ")
is used to get the next tokens until there are no more. - Each token is printed using
printf()
.
Output:
apple
banana
cherry
2. Splitting a String Using Custom Function
In this example, we will manually split a string by iterating through it and extracting substrings based on spaces.
main.c
</>
Copy
#include <stdio.h>
#include <string.h>
void splitString(char *str, char delimiter) {
char *start = str;
while (*str) {
if (*str == delimiter) {
*str = '\0';
printf("%s\n", start);
start = str + 1;
}
str++;
}
printf("%s\n", start);
}
int main() {
char str[] = "apple-banana-cherry-mango";
splitString(str, '-');
return 0;
}
Explanation:
- We define a function
splitString()
that takes a string and a delimiter. - It iterates through the string, replacing delimiters (
'-'
) with'\0'
to break the string. - Each substring is printed when a delimiter is found.
- At the end, the last segment of the string is printed.
Output:
apple
banana
cherry
mango
3. Splitting a String Using Pointers and Manual Parsing
In this example, we will use pointers to split a string into words manually.
main.c
</>
Copy
#include <stdio.h>
#include <ctype.h>
void splitStringManual(char *str) {
char *start = str;
while (*str) {
if (*str == ' ') {
*str = '\0';
printf("%s\n", start);
start = str + 1;
}
str++;
}
printf("%s\n", start);
}
int main() {
char str[] = "apple banana cherry";
splitStringManual(str);
return 0;
}
Explanation:
- We define a function
splitStringManual()
to process the string manually. - A pointer
start
marks the beginning of each word. - When a space is found, it is replaced with
'\0'
to terminate the word. - Each extracted word is printed, and
start
is moved to the next word.
Output:
apple
banana
cherry
Conclusion
In this tutorial, we explored different ways to split a string in C:
- Using
strtok()
: Easiest method for tokenizing strings. - Using a custom function: Manually parsing and replacing delimiters.
- Using pointers: Directly manipulating string data for efficient processing.