How to Split Strings in C

In the C programming language, splitting strings can be achieved using various techniques. Splitting strings is a common operation that involves breaking a larger string into smaller parts based on a specific delimiter or pattern. This can be useful in many scenarios, such as parsing input, tokenizing text, or manipulating data.

Using strtok()

One of the most commonly used functions for splitting strings in C is strtok(). This function is part of the C standard library and allows you to split a string into tokens based on a delimiter. Here’s an example:

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello,World,How,Are,You";
    char *token = strtok(str, ",");
    
    while (token != NULL) {
        printf("%sn", token);
        token = strtok(NULL, ",");
    }
    
    return 0;
}

In this example, the string “Hello,World,How,Are,You” is split into tokens using the comma (‘,’) as the delimiter. The strtok() function returns a pointer to the next token in the string, and by calling it repeatedly with a NULL argument as the first parameter, you can iterate through all the tokens.

Using sscanf()

Another way to split strings in C is by using the sscanf() function. This function is also part of the C standard library and allows you to parse formatted input from a string. Here’s an example:

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello,World,How,Are,You";
    char token[20];
    int i = 0;
    
    while (sscanf(str, "%[^,],", token) == 1) {
        printf("%sn", token);
        str += strlen(token) + 1;
        i++;
    }
    
    return 0;
}

In this example, the sscanf() function is used to extract tokens from the string “Hello,World,How,Are,You” using the “%[^,], ” format specifier. The extracted token is then printed, and the string pointer is updated to point to the next token by adding the length of the token plus one (to skip the delimiter).

Using manual string manipulation

If you prefer a more manual approach, you can split strings in C by manipulating the string directly. Here’s an example:

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello,World,How,Are,You";
    char *token;
    
    token = strtok(str, ",");
    
    while (token != NULL) {
        printf("%sn", token);
        token = strchr(token, ',');
        
        if (token != NULL) {
            *token = '';
            token++;
        }
    }
    
    return 0;
}

In this example, the string “Hello,World,How,Are,You” is split into tokens by replacing the delimiter (‘,’) with a null character (‘’). The strchr() function is used to find the next occurrence of the delimiter, and if found, the delimiter is replaced with a null character and the token pointer is updated to point to the next token.

These are just a few examples of how you can split strings in C. Depending on your specific requirements and the complexity of the task, you may need to choose the most suitable approach. It’s important to handle edge cases, such as empty strings or strings without delimiters, to ensure your program behaves as expected.

Scroll to Top