C Programming

C strsep Function Usage

In every programming language, you can write codes that range from a very basic complexity level to a very high complexity level. Similarly, you also have to deal with multiple different data structures every time you come across a new problem at hand. Strings are also classified as one of the most commonly used data structures, and this is exactly why they have a very large number of different functions associated with them. The C programming language has a dedicated library that deals with all the functions related to strings, and in this particular article, we are going to explore one of the most important and useful functions of this library.

What is the “strsep” Function in C?

The “strsep” function in the C programming language is used to slice the given strings. While writing your code in C, you often come across different lengthy strings that you want to tokenize based upon a given delimiter. In such situations, the “strsep” function comes in handy that does the needful for you. The main purpose of this function is simply to break the given string down into multiple chunks according to the logic of your C program. You can then use these sliced chunks to store them into a database or even use them within the same program for any intended purposes.

Arguments of the “strsep” Function in C:

The typical syntax of the “strsep” function is as follows:

# strsep(StringToBeSliced, Delimiter);

This function accepts two different arguments, as stated in the syntax above. The first argument is the string that is provided as an input to this function that you intend to slice, whereas the second argument is the delimiter according to which you wish to slice the provided string.

To which Library does the “strsep” Function in C belong?

The “strsep” function belongs to the “string.h” library of the C programming language. Once you include this library in your C program, you can conveniently use the “strsep” function within that program.

Implementation Example of the “strsep” Function in C:

In this section, we will be walking you through a very simple use-case of the “strsep” function in C. For understanding that, you need to see the following example code written in C:

In this C program, we have first included the “stdio.h” library, which is the standard header file for the input and output operations in the C programming language. Then, we have included the “string.h” library. This header file actually contains implementing the “strsep” function, as we have already mentioned in this article. Then, we have our “main()” function in which we have first declared two-character type pointers, namely “string” and “chunk”. The first pointer will point to the starting address of the string to be sliced, whereas the second pointer will act as a counter for our loop that is used later in this code. Then, we have the “strings = strdup(“Hello World! I am a string slice function!”)” statement.

We have used the “strdup” function of the “string.h” header file in this statement. This statement simply copies the provided string to the associated variable, i.e. in this case; the provided string will be assigned to the “string” variable that we had declared earlier as a result of using the “strdup” function.

Then, we have our “while” loop that iterates through the “chunk” pointer that will keep pointing to our original string until that string does not become “NULL”. We have used the “strsep” function within this loop that will keep slicing the provided string according to the space “ ” delimiter until there are no characters left in that string or the string becomes “NULL”. Within this “while” loop, we have simply printed the value of the “chunk” variable for each iteration. The “chunk” variable will contain the characters before the specified delimiter for every iteration of the loop. Finally, the code wraps up with the “return 0” statement.

Now, we needed to compile this C code with the command shown below:

$ gcc SliceString.c –o SliceString

Once the code is compiled without generating any error messages, the next step is to execute it with the following command:

$ ./SliceString

You can see in the output of the C program shown in the image below that our provided string has been sliced according to the specified space delimiter, and each word is printed as an independent string on a separate line.

We wanted to see how this function works if we provide a delimiter other than space to this function. Therefore, we modified our same C program slightly, as shown in the following image:

In the image shown above, you can see that our whole program is the same as we have discussed above. The only difference is that we have changed the delimiter from space to an exclamation mark this time.

Upon compiling and executing this C program, our provided string was sliced according to the exclamation mark delimiter, as shown in the image below. All the exclamation marks were removed from the input string in this output while keeping all other characters intact. Moreover, the places from where the exclamation marks were removed from our string introduced new lines in the output.

To explore the working of the “strsep” function with a delimiter other than space further, we tried yet another varied delimiter. For that, we have used the following C code:

In the image shown above, you can see that our whole program is the same as we have discussed above. The only difference is that we have changed the delimiter from an exclamation mark to the character “a” this time.

Upon compiling and executing this C program, our provided string was sliced according to the character “a” delimiter, as shown in the image below. In this output, all the appearances of the “a” character were removed from the input string while keeping all other characters intact. Moreover, the places from where the “a” character was removed from our string introduced new lines in the output.

Conclusion:

This guide mainly intended to talk about the “strsep” function in the C programming language. We briefly introduced you to this function while stating the purpose of using this function in C. Then, we shared the general syntax of this function with you while specifically explaining to you all the arguments that the “strsep” function in C accepts.

After that, we started the C library or the header file to which this particular function belongs. Then, we shared with you a very basic example that makes use of this function in C to slice a given string according to the specified delimiter. Afterwards, we simply discussed the different variations of the same example with you while changing the delimiter every time. Once you understand this whole discussion on the usage of the “strsep” function, you will be able to write efficient programs in C that use this function to slice the given strings.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.