How to Split a String by a Delimiter in C? Last Updated : 17 Jun, 2024 Comments Improve Suggest changes Like Article Like Report 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() method splits str[] according to given delimiters and returns the next token. It needs to be called in a loop to get all tokens. It returns NULL when there are no more tokens. Syntax of strtok()char *strtok(char *str, const char *delims);Parameters str: It is the pointer to the string to be tokenized.delims: It is a string containing all delimiters.Return Value It returns the pointer to the first token encountered in the string.It returns NULL if there are no more tokens found.C program for splitting a stringC Program to demonstrate how to split a string using strtok(). C // C program for splitting a string // using strtok() #include <stdio.h> #include <string.h> int main() { char str[] = "Geeks-for-Geeks-is-a-computer-science-portal"; // Returns first token char* token = strtok(str, " - "); // Keep printing tokens while one of the // delimiters present in str[]. while (token != NULL) { printf(" % s\n", token); token = strtok(NULL, " - "); } return 0; } Output Geeks for Geeks is a computer science portal Time complexity : O(n) Auxiliary Space: O(n), where n is the number of characters in the string. Comment More infoAdvertise with us Next Article How to Split a String by a Delimiter in C? A akshitsaxenaa09 Follow Improve Article Tags : C Programs C Language C-String C Examples Similar Reads How to Split a String by Multiple Delimiters in C? In C, strings are arrays of characters using string manipulation often requires splitting a string into substrings based on multiple delimiters. In this article, we will learn how to split a string by multiple delimiters in C.ExampleInput:char inputString[] = "Hello,World;This|is.GeeksForGeeks";char 2 min read How to Convert an Integer to a String in C? In C, integers can be represented as strings with each digit represented by corresponding numeric character. In this article, we will learn how to convert integers into the stringExamplesInput: 1234Output: "1234"Explanation: The integer 1234 is converted to the string "1234".Input: -567Output: "-567 3 min read How to Create a Dynamic Array of Strings in C? In C, dynamic arrays are essential for handling data structures whose size changes dynamically during the program's runtime. Strings are arrays of characters terminated by the null character '\0'. A dynamic array of strings will ensure to change it's size dynamically during the runtime of the progra 3 min read How to Read a File Line by Line in C? In C, reading a file line by line is a process that involves opening the file, reading its contents line by line until the end of the file is reached, processing each line as needed, and then closing the file.Reading a File Line by Line in CReading a file line by line is a step by step:1. Opening th 3 min read How to Append a Character to a String in C? In this article, we will learn how to append a character to a string using the C program.The most straightforward method to append a character to a string is by using a loop traverse to the end of the string and append the new character manually.C#include <stdio.h> void addChar(char *s, char c 3 min read How to Convert a String to a Char Array in C? In C, the only difference between the string and a character array is possibly the null character '\0' but strings can also be declared as character pointer in which case, its characters are immutable. In this article, we will learn how to convert a string to a char array in C.The most straightforwa 2 min read How to Reverse a String in C? In C, a string is a sequence of characters terminated by a null character (\0). Reversing a string means changing the order of the characters such that the characters at the end of the string come at the start and vice versa. In this article, we will learn how to reverse a string in C. Example: Inpu 2 min read How to Read a Struct from a Binary File in C? The structure in C allows the users to create user-defined data types that can be used to group data items of different types into a single unit. We can save this structure in a file using C file handling. In this article, we will learn how we can read a struct from a binary file in C. Read a Struct 2 min read 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 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 Like