C Programming

Strsep() Function in C Language

Strings are a fundamental part of programming because it is possible to send messages to the user, display the results, and read the files through them. They are also one of the methods of data input and output.

Fluent handling of strings and the functions that process them is very important since we always use them in some part of the program that we develop.

In this Linux Hint article, you will learn how to use the strsep() function to split the strings based on delimiter.

We will show you the syntax and the theoretical description of calling the operation of strsep(), its input and output arguments, and the accepted data types for each of these arguments.

We then apply what we learned in the practical examples that show you how to implement the strsep() function to fragment the strings using the code snippets and images.

Syntax of the Strsep() Function in C Language

char *strsep(char **restrict stringp, const char *restrict delim);

Description of the Strsep() Function in C Language

The strsep() function fragments a string starting from a pre-set delimiter.
This function fragments the string that is pointed to by the stringp input argument and converts its fragments into new strings, all ending with a null character.

Each new string starts at the address that is returned in the output pointer and ends where the strsep() meets the delimiter that is specified in delim. If the function finds a delimiter, it replaces it with a null character, marking the end of the new string.

When the function returns from its call, the stringp pointer is automatically set at the beginning of the fragment, following the delimiter character it found. In this way, the stringp pointer is ready to retrieve the next fragment in the next function call. This process is repeated for each call until strsep() finds no more delimiters and returns a null pointer as a result.

It is important to note that this function irreversibly modifies the input string by replacing the delimiters with null characters.

The strsep() function is defined in the “string.h” header. To use it and the other string management functions, we need to include it in our code as follows:

#include <string.h>

How to Separate a String into Several Fragments Using the Strsep() Function in C Language

In this example, we create a string which contains the “Today is a great day for programming” phrase and use the strsep() function to fragment the contents and output each of the newly obtained strings on a new line in the command console.

The delimiter that we use in the delim input argument is the space character (“ “), so the string that is pointed to by the stringp input argument is fragmented word by word.

In the main() function, we declare the “s_in” string with the “Today is a great day for programming” phrase and the “in_Ptr” pointer. This pointer is the stringp input argument of the strsep() function.

We also declare the “string del” with the space character which is the delim input argument of the strsep() function and the assigned delimiter character.

Finally, we declare the “o_Ptr” pointer which serves as the output argument of strsep(). This pointer points to the retrieved fragment and serves as the input argument to the printf() function to display it on the command console.

Next, we call the strsep() function by passing the “in_Ptr” pointer as the first argument, the del string as the second argument, and the “o_Ptr” pointer as the output argument.

This call is made inside a while loop where the escape condition is that strsep() returns a null pointer which means that there are no more delimiters.

Here is the code for this example:

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

void main ()
{
char s_in [50] = "Today is a great day for programming";
char del [20] = " ";
char* in_Ptr = s_in;
charr* o_Ptr;

while  (o_Ptr != NULL){
        o_Ptr=strsep( &in_Ptr, del);
        printf("%s\n\n", o_Ptr);}
}

As seen in the following figure, strsep() separates the “s_in” string word by word, creating a new string each time, each displayed on a new line in the command console:

Conclusion

In this Linux Hint article, we showed you how to use the strsep() function which is one of the functions that is defined in the “string.h” header to process the strings.

We discussed the theoretical explanation of its operation, its syntax, and its calling method. We also showed you the input and output arguments and the type of data that they each accept.

To help you understand how this function works, we also looked at a practical example where we showed you how to declare the necessary pointers that are used as input and output arguments for this function using the code fragments and snippets which are explained step-by-step, along with the call method.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).