In this article, we will see how to implement strtok() function in C/C++
What is strtok
Strtok is a StringTokenizer function which takes the String and Delimiter as an input parameters and split the string w.r.t to the delimiter and provide the output.
Following is the implementation:
// Author: Murali Krishna
// Email: bmknaidu555@gmail.com
// Message: Comment below if you find any mistakes in this program
#include <iostream>
#pragma warning(disable: 4996)
char * mystrtok(char * string,
const char * delim) {
/* Static storage class is used to keep
track of the string's index in each function call */
static char * index;
// Initial case, where user passes the actual string in strtok
if (string != NULL) {
index = string;
} else {
// further cases, where NULL is passed
string = index;
}
// Final case where the index will be '\0'
if ( * index == '\0') {
return NULL;
}
while ( * index != '\0') {
// Iterate over each delimeter and check if any delimiter matches to the character
for (int i = 0; delim[i] != '\0'; i++) {
if ( * index == delim[i]) {
// We are not intrested in the following case where there is
// no character available to print before delimiter.
// This case occurs when two delimiters are side by side.
if (index == string) {
index++;
string++;
} else {
* index = '\0';
break;
}
}
}
// return the token
if ( * index == '\0') {
index++;
return string;
}
index++;
}
return string;
}
int main(void) {
// Declare a character array and initialize it with the input string.
char string[100] = "a=b;a=c;a=d;a=f;a=g;a=h;a=i;a=j;a=k;";
// String delimiter - To split the string
const char * delimiter = ";";
char * returnS = mystrtok(string, delimiter);
// Call the API in a loop till the return String in NULL
while (returnS != NULL) {
std::cout << returnS << std::endl;
returnS = mystrtok(NULL, delimiter);
}
return 0;
}
Output:
a=b
a=c
a=d
a=f
a=g
a=h
a=i
a=j
a=k