Open In App

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

Last Updated : 05 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn how to split a string into a number of sub-strings using the C program.

The most straightforward method to split a string into substrings using a delimiter is by using strtok() function. Let’s take a look at an example:

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

void split(char *s, char *d) {
  
    // Get the first substring
    char *ss = strtok(s, d);  
    while (ss != NULL) {
        printf("%s\n", ss);
      
        // Get the next token
        ss = strtok(NULL, d);  
    }
}
int main() {
    char s[] = "Geeks,for,Geeks"; 
    char *d = ",";  

    // Call the function to split and
  	// print substrings
    split(s, d);
  
    return 0;
}

Output
Geeks
for
Geeks

Explanation: strtok() splits the string into substrings using a comma as the delimiter. It modifies the original string in-place and returns each token (substring) one by one until there are no more tokens left.

There are also a few other methods in C on how to split a string into a number of sub-strings. Some of them are as follows:

Using a Loop and Manual String Copying

Manually traverse the string, copying each substring into a temporary string whenever we encounter a delimiter. This method doesn’t modify the original string and allows more control over how the substrings are stored.

C
#include <stdio.h>

void split(char *s, char *d) {
    int i = 0, j = 0;
    char ss[50];
  
    while (s[i] != '\0') {
      
        // If current character is not delimiter
        if (s[i] != d[0]) {
          
            // Copy character to ss
            ss[j++] = s[i];  
        } else {
            ss[j] = '\0';  
            printf("%s\n", ss); 
          
            // Reset temp index for next substring
            j = 0;  
        }
        i++;
    }

    // Print the last substring
    if (j > 0) {  
        ss[j] = '\0';
        printf("%s\n", ss);
    }
}

int main() {
    char s[] = "Geeks,for,Geeks"; 
    char *d = ",";

    split(s, d);

    return 0;
}

Output
Geeks
for
Geeks

By Replacing Delimiter by Null Character

Replace delimiter by null character ‘\0’ and print the substring till this null character. After that, move pointer after this null character and print the substring till next null character. Keep doing this till the end of the string.

C
#include <stdio.h>

void split(char *s, char *d) {
  
    // Pointer to start of current substring
    char *ss = s;

    // Loop through the string
    while (*s) {
      
        // If the delimiter is found
        if (*s == d[0]) {  
          
            // Replace delimiter with null character
            *s = '\0';
            printf("%s\n", ss);
            ss = s + 1;
        }
        s++;
    }

    // Print the last substring
    if (*ss != '\0')
        printf("%s\n", ss);
}

int main() {
    char s[] = "Geeks,for,Geeks"; 
    char *d = ",";

    split(s, d);

    return 0;
}

Output
Geeks
for
Geeks


Next Article

Similar Reads