In this article, we will see how to implement strtok() function in c/c++

// Author: Murali Krishna
// Email: bmknaidu555@gmail.com
// Message: Kindly let me know if you see 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) {

  char string[100] = "a=b;a=c;a=d;a=f;a=g;a=h;a=i;a=j;a=k;";
  const char * delimiter = ";";
  char * returnS = mystrtok(string, delimiter);

  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