Easy Tutorial
❮ C Examples Matrix Transpose C Function Setlocale ❯

C Library Function - strtok()

C Standard Library - <string.h>

Description

The C library function char *strtok(char *str, const char *delim) splits the string str into a series of substrings, with delim as the delimiter.

Declaration

Below is the declaration for the strtok() function.

char *strtok(char *str, const char *delim)

Parameters

Return Value

The function returns the first substring resulting from the split. If no more substrings are found, it returns a null pointer.

Example

The following example demonstrates the use of the strtok() function.

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

int main () {
   char str[80] = "This is - www.tutorialpro.org - website";
   const char s[2] = "-";
   char *token;

   /* Get the first substring */
   token = strtok(str, s);

   /* Continue to get the other substrings */
   while( token != NULL ) {
      printf( "%s\n", token );

      token = strtok(NULL, s);
   }

   return(0);
}

Let's compile and run the above program, which will produce the following result:

This is 
 www.tutorialpro.org 
 website

C Standard Library - <string.h>

❮ C Examples Matrix Transpose C Function Setlocale ❯