Implementing of strtok() function in C++

Implementing of strtok() function in C++

Implementing of strtok() function in C++. ​It is present in the header file “string.h” and returns a pointer to the next token if present, if the next token is not present it returns NULL. To get all the tokens the idea is to call this function in a loop.

The strtok() function is used in tokenizing a string based on a delimiter. It is present in the header file “string.h” and returns a pointer to the next token if present, if the next token is not present it returns NULL. To get all the tokens the idea is to call this function in a loop.

Header File:

#include <string.h>

Syntax:

char *strtok(char *s1, const char *s2);

In this article, we will discuss the implementation of this function in which two things must be taken into consideration:

  • Maintain the state of the string to make sure how many tokens we have already extracted.
  • Secondly, maintain the list of extracted tokens in an array to return it.

Steps:

  • Create a function strtok() which accepts string and delimiter as an argument and return char pointer.
  • Create a static variable input to maintain the state of the string.
  • Check if extracting the tokens for the first time then initialize the input with it.
  • If the input is NULL and all the tokens are extracted then return NULL.
  • In this step, start extracting tokens and store them in the array result[].
  • Now, iterate a loop until NULL occurs or the delimiter then return the result by including ‘\0’.
  • When the end of the string is reached and if it requires then manually add a ‘\0‘ and include this corner case in the end.

c++ c++ programs strings cpp-library cpp-string

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

C programming for Beginners | Learn C Programming | C Language

C Language is an evergreen language and is used widely across different industries, This C programming is a must for students and working professionals to become a great Software Engineer especially when they are working in Software Development Domain. Great Learning brings you this live session on "Introduction to C". In this live session, we will be covering major concepts in C Programming such as Different Variables, Different Data Types that are being used, its Operators, Flow control statements, Structure, and lot more.

Learning C: Input and Output and Two Program Templates

Learning C: Input and Output and Two Program Templates. Besides demonstrating how to perform input and output in C, I will also be demonstrating two templates that are related to those topics — Prompt, Then Read and Input, Process, Output (IPO). The IPO template, in particular, is important because practically every C program you write will use this template.

Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.

Classes and Objects in C++ | C++ Programming Language - Part2

This Edureka video Classes and Objects in C++ will help you to get started with C++.

Introduction to C++ Programming Language | C++ Tutorial for Beginners - Part 1

This Edureka video on Introduction to C++ Programming will help you to get started with C++.