AppDividend
Latest Code Tutorials

C++ strtok() Function Example | strtok() in C++

0

C++ strtok() is an inbuilt function that is used for splitting a string based on a delimiter.  C++ strtok() function works on the concept of the token. It returns the next token of the string, which is terminated by a null symbol. The strtok() function in C++ returns the next token in a null-terminated byte string.

C++ strtok()

The C library function char *strtok(char *str, const char *delim) breaks string str into a series of tokens using the delimiter delim.

Syntax

char* strtok(char* str_var, const char* delimiter);

The above function is called much time to obtain successive tokens so that we can put the delimiter.

Generally, there are two cases when the passed string is null and when the given string is not null.

When the string is null, and then when we call strtok(), the function continues from where it left in the previous invocation.

When the string is not null, then strtok() is considered to be the first call. The function then searches for the first character that is not contained by the delimiter.

If unable to find any character, then it is said that the string doesn’t contain any token, and at last, a null pointer is returned.

 If the character is found, then the function searches for a character that is present in the delimiter. If there is no separator str_var is the only token; otherwise, it is replaced by a ‘\0’, and the pointer is stored in subsequent invocations. In last, the function returns a pointer to the beginning of the token.

Parameter

It takes two parameters str_var and delimiter. The str_var is the main string variable, and the delimiter is the separator characters on which the str_var will be separated.

Return Value

It returns the next token in the iterative process.

Example programs on strtok() function  in C++

Example 1: Write a program to show the working of strtok() function by splitting the string based on a comma ”.”

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

using namespace std;

int main()
{
  char str_var[] = "Hello,World,I,am,AxeaBot";
  char *token = strtok(str_var, ",");
  while (token != NULL)
  {
    cout << token << endl;
    token = strtok(NULL, ",");
  }
  return 0;
}

Output

Hello
World
I
am
AxeaBot

Example 2: Write a program to check if the token is null or not.

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

using namespace std;

int main()
{
  char str_var[] = "Hello,World,I,am,AxeaBot";
  char *token = strtok(str_var, ",");
  while (token != NULL)
  {
    cout << token << endl;
    token = strtok(NULL, ",");
  }
  if (token == NULL)
  {
    cout << "Now the token is null !! ";
  }
  return 0;
}

Output

Hello
World
I
am
AxeaBot	
Now the token is null !!

See also

C++ strspn()

C++ strpbrk()

C++ strcspn()

C++ strchr()

C++ memchr()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.