Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
C++ Tutorial
C++ Language Tutorial - Object Oriented Programing - OOPs History of C++
C++ Language Tutorial - Object Oriented Programing - OOPs Structure of C++
C++ Language Tutorial - Object Oriented Programing - OOPs C++ Datatypes
C++ Language Tutorial - Object Oriented Programing - OOPs Variables
C++ Language Tutorial - Object Oriented Programing - OOPs Constants
C++ Language Tutorial - Object Oriented Programing - OOPs Operators
C++ Language Tutorial - Object Oriented Programing - OOPs Control Structures
C++ Language Tutorial - Object Oriented Programing - OOPs Arrays
C++ Language Tutorial - Object Oriented Programing - OOPs Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Class
C++ Language Tutorial - Object Oriented Programing - OOPs Predefined Functions
C++ Language Tutorial - Object Oriented Programing - OOPs I/O Functions
C++ Language Tutorial - Object Oriented Programing - OOPs String, Character Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Mathematical Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Time Date Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Dynamic Allocation
C++ Language Tutorial - Object Oriented Programing - OOPs Utility Functions
C++ Language Tutorial - Object Oriented Programing - OOPs OOP's Concept
C++ Language Tutorial - Object Oriented Programing - OOPs Special Topics
C++ Language Tutorial - Object Oriented Programing - OOPs Type casting
C++ Language Tutorial - Object Oriented Programing - OOPs Feedback
C++ Language Tutorial - Object Oriented Programing - OOPs Ask Your Doubts
 




strtok() - Manipulation Function


Tutorials Cpp

Topic

How to split a string into tokens in C++?



Explanation

strtok() manipulation function is used to split a string into tokens. It returns a pointer to next token in the "str1", the characters of "str2" are the delimiters for the token. Function returns a NULL pointer if there are no tokens to return. When this function is called for the first time, "str1" point to string being tokenized and then a NULL pointer is used for "str1". Different delimiters can be used every time the strok() is called.

Syntax:
    char *strtok (char *str1, const char *str2);

Example:

   #include <stdio.h>
   #include <cstring.h>
   int main ()
    {
      char str1[] = "Its* time*have* fun";
      char str2[] = "*";
      char * pnt;
      pnt=strtok( str1, str2 );
      while( pnt!= NULL ) 
       {
         printf( "Tokenized string using *  is:: %s\n", pnt );
         pnt = strtok( NULL, str2 );
       }
     }

Result:
   Tokenized string using *  is:: Its
   Tokenized string using *  is:: time
   Tokenized string using *  is:: have
   Tokenized string using *  is:: fun

In the above example, "strtok()" manipulation function is used tokenize based on the delimiter "*". It splits it into tokens by the delimiters everytime it is called.










Note
C++ is one of the most used programming languages in the world. Also known as "C with Classes". Hope you enjoy this tutorial. Do send your feedback or suggestions on this C++ tutorial. This is a copyright content.

Other Links

web hosting