
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What is strtok() function in C language?
strtok() function is a part of the <string.h> header file #include <string.h>
The syntax of strtok() function is as follows −
char* strtok(char* string, const char* limiter);
Input string string and a delimiter character limiter. strtok() will divide the string into tokens based on the delimited character.
We can expect a list of strings from strtok(). But, the function returns a single string because after calling strtok(input, limiter), it will returns the first token.
But we have to keep calling the function again and again on a NULL input string, until we get NULL! Generally, we used to keep calling strtok(NULL, delim) until it returns NULL.
Example
Following is the C program for strtok() function −
#include <stdio.h> #include <string.h> int main() { char input_string[] = "Hello Tutorials Point!"; char token_list[20][20]; char* token = strtok(input_string, " "); int num_tokens = 0; // Index to token list. We will append to the list while (token != NULL) { strcpy(token_list[num_tokens], token); // Copy to token list num_tokens++; token = strtok(NULL, " "); // Get the next token. Notice that input=NULL now! } // Print the list of tokens printf("Token List:
"); for (int i=0; i < num_tokens; i++) { printf("%s
", token_list[i]); } return 0; }
Output
When the above program is executed, it produces the following result −
Token List: Hello Tutorials Point!
Input string is “Hello Tutorials Point”, and we are trying to tokenize it by spaces.
We get first token by using strtok(input, " "). Here the double quotes are delimiter and are a single character string!
Afterwards, we keep getting tokens by using strtok(NULL, " ") and loop until we get NULL from strtok().
- Related Articles
- strtok() function in PHP
- What is strncpy() Function in C language?
- What is strrev() Function in C language?
- What is strlen function in C language?
- What is strcoll() Function in C language?
- What is strspn() Function in C language?
- What is strcpy() Function in C language?
- What is strcat() Function in C language?
- What is strncat() Function in C language?
- What is strcmp() Function in C language?
- What is strncmp() Function in C language?
- What is strstr() Function in C language?
- What is function prototype in C language
- What is exit() function in C language?
- What is strtok_r() function in C language?
