Skip to main content

Command Palette

Search for a command to run...

Tokenizing a string

Published
•2 min read•View as Markdown

Syntax of strtok() in C

The syntax of function strtok in c Language is given below:

char *strtok(char *string, const char *delimiter)

Parameters of strtok() in C

string − This is the string which is passed as the parameter which is to be modified by this function.

delimiter − This is the string containing the delimiter on the basis of which string will be broken into series of tokens.And these may vary from one call to another.

Return Value of strtok() in C

The return value of this function is a pointer to the first token which is found in the string on breaking it into several tokens. And if there will be no tokens left to retrieve, a null pointer is returned.

Example

A program which splits a string using function strtok in c.

#include <stdio.h>
#include <string.h>

int main()
{
char str[] = "country,adjusted_satisfaction,avg_satisfaction,std_satisfaction,"
            "avg_income,median_income,income_inequality,region,happyScore,GDP";

// Returns the first token
char* token = strtok(str, ",");

// Keeps printing the token till the token is not NULL.
int i=0;
while (token != NULL)
{
    printf("Column %d is %s\n",i,token);
    token = strtok(NULL, ",");
    i++;
}

return 0;

}

Output:

Explanation:

The first call to strtok must pass the C string to tokenize, and subsequent calls must specify NULL as the first argument, which tells the function to continue tokenizing the string you passed in first.

The return value of the function returns a C string that is the current token retrieved. So first call --> first token, second call (with NULL specified) --> second token, and so on.

When there are no tokens left to retrieve, strtok returns NULL, meaning that the string has been fully tokenized.

strtok() stores the pointer in static variable where did you last time left off , so on its 2nd call , when we pass the null , strtok() gets the pointer from the static variable .

If you provide the same string name , it again starts from beginning.

Moreover strtok() is destructive i.e. it make changes to the orignal string. so make sure you always have a copy of orignal one.

22 views

More from this blog

P

Programming Tutorials

89 posts