C Program to Split a String into a Number of Sub-Strings Last Updated : 05 Dec, 2024 Summarize Comments Improve Suggest changes Share 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; } OutputGeeks 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 CopyingManually 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; } OutputGeeks for Geeks By Replacing Delimiter by Null CharacterReplace 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; } OutputGeeks for Geeks Comment More infoAdvertise with us Next Article C Program to Split a String into a Number of Sub-Strings K kartik Follow Improve Article Tags : C Programs C Language C Strings Programs Similar Reads Array of Pointers to Strings in C In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings 2 min read How to Split a String by a Delimiter in C? Splitting a string by a delimiter is a common task in programming. In C, strings are arrays of characters, and there are several ways to split them based on a delimiter. In this article, we will explore different methods to split a string by a delimiter in C. Splitting Strings in CThe strtok() metho 2 min read How to Match a Pattern in a String in C? Matching a pattern in a string involves searching for a specific sequence of characters within a larger string. In this article, we will learn different methods to efficiently match patterns in a string in C.The most straightforward method to match a string is by using strstr() function. Letâs take 3 min read How to Find Length of a String Without string.h and Loop in C? C language provides the library for common string operations including string length. Otherwise, a loop can be used to count string length. But is there other ways to find the string length in C apart from above two methods. In this article, we will learn how to find length of a string without strin 2 min read C program to input an array from a sequence of space-separated integers Given a string S consisting of space-separated integers, the task is to write a C program to take the integers as input from the string S and store them in an array arr[]. Examples: Input: S = "1 2 3 4"Output: {1, 2, 3, 4} Input: S = "32 12"Output: {32, 12} Approach: The idea is to solve the given p 2 min read Like