Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

C Program to Split a String into a Number of Sub-Strings

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. 
Almost all programming languages, provide a function split a string by some delimiter. 

In C:  

// Splits str[] according to given delimiters.
// and returns next token. It needs to be called
// in a loop to get all tokens. It returns NULL
// when there are no more tokens.
char * strtok(char str[], const char *delims);


// C program for splitting a string
// using strtok()
#include <stdio.h>
#include <string.h>
int main()
    char str[] = "Geeks-for-Geeks";
    // Returns first token
    char* token = strtok(str, "-");
    // Keep printing tokens while one of the
    // delimiters present in str[].
    while (token != NULL) {
        printf("%s", token);
        token = strtok(NULL, "-");
    return 0;

Output: Geeks
My Personal Notes arrow_drop_up
Last Updated : 26 Oct, 2022
Like Article
Save Article
Similar Reads