“The strtok() function is a predefined C library function that enables us to break strings into multiple strings or zero, the most important point while splitting strings to keep in mind is that it can’t be empty or null.
Two parameters are passed to the strtok() function that is responsible for tokenizing the string; the first parameter is the on that holds the string that is to be tokenized, while the second one is the delimiter that holds the keyword or character which will define the start and end of the string at which it is tokenized. The strtok() function ignores the delimiter part and simply displays the string that is right next to the delimiter.
Whenever the strtok() function is called, the pointer to the next token, which is denoted by a null-terminated string, is returned. When it accounts for the end of the string where no token is discovered, it will return null.”
Syntax:m
In the syntax mentioned above, two arguments are passed: the first parameter, “string,” is the one that is the main string that is to be parsed, whereas the second parameter, “token,” is the delimiter that will define the breaking point of the string.
Let’s suppose when the strtok() function is called for the first time, it will start searching for the character delimiter step by step. As we know, strings are stored in the form of an array that allocates consecutive memory addresses to store the one-by-one pointer that will check for the delimiter. In case the delimiter is found, it will break the string into segments; if no delimiter is found, it will search for the terminator or the null value, which will indicate the end point of the string, then it will return the null value.
Once the string is split, it can’t be converted to its original state. To prevent this, we have to first copy the string into the buffer, and then we will pass the address of the buffer to the strtok() so that through this, we can preserve our original string.
Example 1
In this example, we are going to implement the strtok() function to break the single string into multiple segments. Moving to our first step in which, we declare header files “string.h” and “stdio.h”. As we are going to work with strings, so it is necessary to include a string header file unless we are not able to implement strings. Then moving to our main function, where we declared 3 character arrays that are string[], del[], and the character variable named my token.
String[] is the character array that is responsible for holding a string of characters which will be parsed to break it into parts. The dell[] is the constant character array that works as a delimiter, or we can say it is the character that will define the pointer, at which point it should break the string; in our case, the delimiter is “comma”. Where the third variable, mytoken, is responsible for storing the return value of the strtok() function.
#include <stdio.h>
int main () {
char string[] = "my new project,my strtok project";
const char del[] = ",";
char *mytoken;
mytoken = strtok(string, del);
while( mytoken != NULL ) {
printf( " %s\n", mytoken );
mytoken = strtok(NULL, del);
}
return(0);
}
After successfully declaring our variables, we will assign mytoken variables the strtok() function in which we will be passing the variables as the parameter that we have already declared above, the string array, and the delimiter variable. Then we created a while loop in which we passed our mytoken variable, which is holding the return value of the strtok() function. While loop will check the condition we applied; if the mytoken is not null, it will move into the inner part of the while loop; otherwise, it will not display any execution.
Let’s suppose our condition is true; then, it will print the string that is being split into segments. Then again, assigning mytoken a strtok() function in which now we will pass the Null element and the delimiter. Null is used to instruct the strtok() function to stop the parsing and return null /0 whenever the string is ended, or a null variable is discovered.
Until the whole string is fetched for delimiters and broken down into strings according to the delimiter defined, the while loop will not stop working. It will print the string right next to every delimiter and ends whenever the delimiter is discovered. In our case, we only used one delimiter in our sentence, so it just breaks into two segments, as shown in the figure below.
Example 2
In this example, we will be splitting string arrays into multiple segments using spaces as our delimiter. In the above example, we just split two strings into segments, but in this, we will use spaces as our delimiter and will break a string into multiple segments.
#include<string.h>
int main()
{
char string[] = "My strtok function in C language";
char *strtoken;
strtoken = strtok(string, " ");
while(strtoken != NULL)
{
printf("%s \n", strtoken);
strtoken = strtok(NULL, " ");
}
return 0;
}
As shown in the code above, we just included our header file; after including of header file, we will move into our main function, where we will declare two character variables in which one will be responsible for holding the string and the second one that will be responsible for holding return value of strtok() function. In this case, we have the “string[]” variable that stores the string that is to be split, whereas the “strtoken” is used to store the return value of the strtok() function.
Then we will call the strtok() function, which we pass to parameters, then, one is the string that is to be parsed, and the second one is the delimiter itself; in this example, the delimiter is space. As shown in the figure below, whenever the space is discovered, it will split the string into segments. The delimiter will not be printed the first letter to the end where the next delimiter will be found; it will print that whole string between delimiters.
Conclusion
In this guide, we have studied the strtok() function briefly, in which we described the purpose of the strtok() function as well as its implementation and how we will split a single string into multiple segments using a single character variable that is the delimiter. strtok() function is easy to use and interpret strings. The purpose of our article is to make it easy for you to learn the strtok() function by definition as well as by multiple examples.