Split a String by Comma in C
In C, we can split a string by commas using functions like strtok()
, manual parsing with loops, and other techniques. Since C does not have built-in support for high-level string manipulation like Python or JavaScript, we need to use C string functions from the string.h
library or implement custom logic for string splitting.
Examples to Split a String by Comma
1. Using strtok()
to Split a String
In this example, we will use the strtok()
function to tokenize a string by comma. strtok()
is part of string.h
and helps break a string into smaller tokens based on a delimiter.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "apple,banana,grape,orange";
char *token = strtok(str, ",");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, ",");
}
return 0;
}
Explanation:
- We define a character array
str
containing a comma-separated string. - We use
strtok(str, ",")
to get the first token (word before the first comma). - The
while
loop iterates untilstrtok()
returnsNULL
, meaning no more tokens are found. - Each token is printed using
printf()
, and we continue tokenizing usingstrtok(NULL, ",")
.
Output:
apple
banana
grape
orange
2. Splitting a String Without Modifying the Original String
The strtok()
function modifies the original string. To keep the original string unchanged, we use strcpy()
to copy the string to a new buffer before splitting.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "red,green,blue,yellow";
char copy[50];
strcpy(copy, str);
char *token = strtok(copy, ",");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, ",");
}
printf("\nOriginal String: %s\n", str);
return 0;
}
Explanation:
- We define a character array
str
and a separate buffercopy
. - We use
strcpy(copy, str)
to duplicate the original string. - Tokenization happens on
copy
, preservingstr
intact. - The loop prints each token while ensuring the original string remains unchanged.
Output:
red
green
blue
yellow
Original String: red,green,blue,yellow
3. Splitting a String Using a Manual Loop
If we cannot use strtok()
, we can manually extract tokens by iterating over the string and checking for commas.
main.c
#include <stdio.h>
void splitString(const char *str) {
int start = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == ',') {
printf("%.*s\n", i - start, str + start);
start = i + 1;
}
}
printf("%s\n", str + start);
}
int main() {
char str[] = "dog,cat,bird,fish";
splitString(str);
return 0;
}
Explanation:
- We define a function
splitString()
that takes a string as input. - We iterate over the string, looking for commas.
- Each substring before a comma is printed using
printf("%.*s\n")
, which prints a substring of lengthi - start
. - After the loop, the last portion of the string is printed.
Output:
dog
cat
bird
fish
Conclusion
In this tutorial, we explored different ways to split a string by commas in C:
- Using
strtok()
: The easiest way to tokenize a string. - Preserving the original string: We used
strcpy()
to avoid modifying the input string. - Manual string parsing: We iterated through the string to extract tokens without using
strtok()
.