Strings : Strtok Function


String Strtok Functions at HiringLibrary-Com

Write your own implementation of the strtok function.

Rationale : The algorithm we will be using performs the following steps:

  • Store the contents of the initial string in a static variable, call it initial.
  • At each recursive call of the function work with the value of this static variable.
  • Find the first character that matches a delimiter and end the string at that position.
  • Return the modified string.


Before solving this problem do the following (not limited to):

  • Clarify doubts
  • Write/loudly speak/analyze single or multiple pseudocode(e.g. trivial algorithm to advanced algorithm/Data Structures) which can lead to solution
  • Explain any assumptions or limitations on the written pseudocode
  • Mention major data structures to be used
  • Also how the positive/negative functional tests prove that the resultant solution is correct. 

Professional Teacher at

Clarification Points 

Clarification Point-1:


What is the maximum size of the given input string? What kind of characters can it contain?


Diversity in WorkPlace - Japanese,Korea,Chinese

Pseudo Code

Pseudo code :

Mystrtok(s, delimiter)
  If first call
    Initial = s
  Word = initial
  For i = 0 to word.length
    If word[i] is a character of delimiter
       Word[i] = 0
    While word[i] is still a character of delimiter
    Initial = initial + I;
  Return word;

How did you find your first job?



  • We will assume our given string is a sentence of words separated by a single space character.
  • We will also assume the given string can store up to 200 characters.

Career Summary from a Professional for HiringLibrar

Major Data Structures 

Major data structures:

Array of characters (string)

Example at Hiring Library

Unit tests:  
Postive Functional Test cases  
Input Expected Output
S=”string” “string”
S=”this is a test string”

S=”just     three   words”

“this is a test string”

“just three words”


Negative Functional Test Cases
S=string with any character other than lower case / upper case letters and spaces Undefined

Answer of the coding Problem

Here is the sample answer that implements the solution described above

#include <cstdio>
#include <string.h>

using namespace std;

char s[201];

char *mystrtok(char *s, char *delimiter)
  // we need a static variable to save current state
    static char *initial;

    char * word = new char[201];

    // if first call (a call with an actual string, not with NULL)
    if (s != NULL)
        initial = s;

        // check input for validity
        for (int stringIndex = 0; stringIndex < strlen(s); stringIndex++)
      if (s[stringIndex] != ' ' && !(s[stringIndex] >= 'a' &&s[stringIndex] <= 'z')
          &&!(s[stringIndex] >= 'A' && s[stringIndex] <= 'Z'))
        printf("Invalid input!\n");
        return "";

    if (initial == NULL) return NULL;

    strcpy(word, initial);

    // in the last step, initial will have no more delimiter characters
    // so the program will not enter the if condition
    // we need to take this into account
    bool enteredloop = false;

    for (int i = 0; i < strlen(word); i++)
        if(strchr(delimiter, word[i]))
            word[i] = 0;
            while(strchr(delimiter, initial[i])) i++;
            initial = initial + i;
            enteredloop = true;

    if (!enteredloop) initial = NULL;

    return word;

int main()
  strcpy(s, "test   string for  mystrtok");

  char *word = mystrtok(s, "! ");

  printf("The string delimited by space:\n");
    printf("%s ", word);
    word = mystrtok(NULL, "! ");

  return 0;

Output at HiringLibrary-Com

Output and Further Excercise

Further Exercise at HiringLibrary

Output: Further exercise:
test   string for     mystrtok

The string delimited by space:

test string for mystrtok

Q: How do you implement strchr?

A: The idea is similar, but we need to return the position at which the first character is found in the given string. Furthermore, at each subsequent call, we return the next position. We can use the same method (a static char array) to ‘remember’ the current state of the string.