Strsep Function In C

This guide will show you how to split strings based on delimiters using the strsep function.

We'll discuss the parameters and return values of strsep, the syntax, theoretical overview, and the valid data types for each of these parameters.

We then apply our acquired knowledge by implementing code excerpts and visuals to split strings using the strsep function, as shown in the hands-on illustrations.

Strsep Function Syntax in the C Language:

Example

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

An explanation of the Strsep function in C:

  • A string is broken apart using the strsep function , beginning with a pre-determined delimiter.
  • The string pointed to by the stringp input argument is split up by this function, and the pieces are then combined to form new strings that all end in the null character.
  • Each new string begins at the address given by the outpuLogic Practiceer and terminates where the delimiter specified by delim and strsep When a delimiter is found, the function substitutes it with a null character to indicate the end of the new string.
  • The stringp pointer is automatically set at the start of the fragment after the delimiter character is discovered when the function returns from its call. The stringp pointer is now prepared to obtain the next fragment in the following function call. Until strsep runs out of delimiters and returns a null pointer. As a result, this process is repeated for each call.

It's important to highlight that this function permanently modifies the input text by replacing null characters with the delimiters. The strsep function is defined in the "string.h" header. To utilize this function along with other string handling functions, we need to integrate it into our code in the following manner:

Example

#include <string.h>

How to Use the C Language's Strsep Function to Split a String into Several Fragments

In this instance, the expression "Commence coding" is formulated as a string, and it is disintegrated utilizing the strsep method. Each of the freshly generated strings is subsequently displayed individually in the command prompt. The string indicated by the stringp parameter is disassembled word by word as the space character (" ") is designated as the delimiter in the delim argument.

We initialize the string "sin" containing the phrase "Commence coding" and the pointer "inPtr" within the main function. The strsep function takes this pointer as the input argument named stringp.

When using the delim parameter in the strsep function along with the specified delimiter character, we define the "string del" variable with a space character. Following this, we initialize the "o_Ptr" pointer, which serves as the output parameter for strsep. To showcase the extracted segment on the terminal, the printf function will accept this pointer as an argument.

Following this, providing the del string as the initial parameter, the "inPtr" pointer as the second parameter, and the "oPtr" pointer as the output parameter, we invoke the strsep function. This invocation occurs within a while loop, which continues until strsep yields a null pointer signifying the absence of additional delimiters.

The code for this example is as follows:

Example

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

int main()
{
    char s_in[50] = "Let's start programming ";
    char del[20] = " ";
    char* in_Ptr = s_in;
    char* o_Ptr;

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

    return 0;
}

strsep function divides the "s_in" string into individual words, generating a new string with each iteration. Subsequently, each word is printed on a separate line within the terminal window, as illustrated below:

Output:

Output

Let's
start
programming

Conclusion

In this guide, we illustrated the process of manipulating strings through the utilization of the strsep function, which is among the functions defined in the "string.h" header file. We delved into the rationale behind its syntax, method of invocation, and functionality. Furthermore, we showcased the types of data that can be handled by each input and output parameter.

Input Required

This code uses input(). Please provide values below: