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

</>
Copy
#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:

  1. We define a character array str containing a comma-separated string.
  2. We use strtok(str, ",") to get the first token (word before the first comma).
  3. The while loop iterates until strtok() returns NULL, meaning no more tokens are found.
  4. Each token is printed using printf(), and we continue tokenizing using strtok(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

</>
Copy
#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:

  1. We define a character array str and a separate buffer copy.
  2. We use strcpy(copy, str) to duplicate the original string.
  3. Tokenization happens on copy, preserving str intact.
  4. 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

</>
Copy
#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:

  1. We define a function splitString() that takes a string as input.
  2. We iterate over the string, looking for commas.
  3. Each substring before a comma is printed using printf("%.*s\n"), which prints a substring of length i - start.
  4. 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:

  1. Using strtok(): The easiest way to tokenize a string.
  2. Preserving the original string: We used strcpy() to avoid modifying the input string.
  3. Manual string parsing: We iterated through the string to extract tokens without using strtok().