It has the following syntax:
Char *strtok(char *s, const char *delimits);
In this particular format,
- s: denotes the pointer directing to the string requiring tokenization.
- delimits: signifies a string encompassing all the delimiters.
C Strtok function Example using commas
Let's consider an example to demonstrate the utilization of the strtok function with comma delimiters in the C programming language.
Example
#include <stdio.h>
#include <string.h>
int main( ) {
char s[] = "Java, C++, JavaScript";
char *tokenValue;
// Calling the strok( ) method to get the first token
tokenValue = strtok(s, ",");
// Executed the while loop over the string to extract all remaining tokens
printf("After splitting the char array values, we get: \n");
while(tokenValue != NULL) {
printf("%s\n", tokenValue );
tokenValue = strtok( NULL, ",");
}
return 0;
}
Output:
After splitting the char array values, we get:
Java
C++
JavaScript
Explanation:
In this instance, we showcase the application of the strtok function in C for dividing a string into tokens based on a specified delimiter. In this case, we've selected the string "Java, C++, JavaScript" and segmented it by the comma (,) with each token being displayed individually through a while loop.
Complexity Analysis
The complexity analysis of this program is as follows:
Time Complexity
The total time complexity of this program amounts to O(n).
Space Complexity
The total space complexity of the aforementioned code is O(n) as a result of storing the input string.
C Strtok function example to split a string using Spaces
Let's consider a scenario to demonstrate the strtok function in C, utilizing spaces.
Example
#include <stdio.h>
#include <string.h>
int main( ) {
// Initialized a character array
char s[] = "Logic PracticeTech is one of the Best Tutorial Websites";
// Initialized a character array for delimiter
const char delim[2]=" ";
// Declared a character value for getting the token values
char *tokenValue;
// Invoking the strok () method to get the first token value
tokenValue = strtok(s, " ");
printf("After splitting the char array values, we get: \n");
// Executing the while loop to fetch the leftover token values
while(tokenValue != NULL) {
printf("%s \n", tokenValue);
tokenValue = strtok(NULL, delim);
}
return 0;
}
Output:
After splitting the char array values, we get:
Logic PracticeTech
is
one
of
the
Best
Tutorial
Websites
Explanation:
In this illustration, we showcase the utilization of the strtok function in C for dividing a string into tokens with spaces as separators. The given string "Logic PracticeTech is one of the Best Tutorial Websites" is segmented into individual words. The iteration in the while loop persists until all tokens are extracted, and each token is displayed on a fresh line. This method aids in handling words independently within a sentence.
Complexity Analysis
The examination of the complexity of this program is outlined below:
Time Complexity
The total time complexity for the aforementioned code is of the order O(n).
Space Complexity
The total spatial complexity of the aforementioned code is O(n) as a result of the necessity to store the input string.
Features of the Strtok function in C
There are several features of the strtok function in C. Some of them are as follows:
- The strtok function should not be utilized in multi-threaded programs without proper synchronization because it is not thread-safe.
- strtoktreats a string of consecutive delimiter characters as a single delimiter and does not return an empty token for each one.
- Strtok may not function properly with non-ASCII characters, especially if the input string is encoded in a multi-byte character set, so use caution when using it. We can use a different tokenization method in this situation.
- Strtokreturns a NULL pointer after all the tokens have been extracted from the input string. It can be used as a signal to halt token processing.
- The strtok functionsubstitutes null ('0') characters for the input string's delimiter characters. As the original string has been altered, subsequent calls to strtok will follow up where the first one left off.
- Multiple delimiters can be handled by the function by passing a string containing each delimiter as the second argument to the strtok function.
Conclusion
In summary, the strtok function within C programming plays a crucial role in dividing strings into smaller tokens using defined delimiters like spaces, punctuation marks, and other special symbols. It alters the original string and provides each token in sequence with subsequent function calls. While strtok is straightforward and effective, it lacks thread safety and directly changes input strings. This function is primarily applied in tasks involving string parsing, text manipulation, processing command-line arguments, and extracting data.
Strtok function FAQs
1) How does the 'strtok' function work in C?
It takes the following steps to work in the strtok function.
- On the first call, it passes the string to tokenize.
- The method returns a pointer to the first token that is found.
- On subsequent calls, pass 'NULL' as the first argument to continue tokenizing the same string.
- It modifies the original string by replacing the delimiters with the '\0', i.e., null terminator.
Yes, it is possible to utilize the 'strtok' function in multithreaded environments in the C programming language.
The 'strtok' function is considered non-thread-safe due to its reliance on internal static state. To ensure thread safety during tokenization, 'strtok_r' serves as the reentrant alternative.
In C, when a string contains consecutive delimiters, the strtok function will consider them as one single delimiter. This means that the function will treat multiple consecutive delimiters as if they were a single delimiter.
In the C programming language, the 'strtok' function considers consecutive delimiters as a single delimiter, thus bypassing them and not producing empty tokens.
4) In C programming, consider the scenario where the string begins with a special character.
When a string starts with a delimiter, the initial delimiters are disregarded, and the initial token retrieved will be the first non-delimiter substring.
To tokenize a string without altering the original one, we can employ techniques such as utilizing regular expressions or dedicated tokenization libraries that are specifically designed for this purpose. These methods allow us to break down the string into individual tokens while keeping the original string intact.
We have the option to duplicate the string by employing the 'strdup' function or 'strcpy' function, followed by utilizing the 'strtok' function on the duplicated string.
6) Is the original string altered by the 'strtok' function in C programming?
Yes, the strtok function replaces the delimiter characters with '\0' to mark the end of the tokens.