• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

How strtok() Works

April 13, 2015 by Dhaval Dave

strtok() : Split string into tokens
Syntax :

char * strtok ( char * str, const char * delimiters );

Parameters
str C string to truncate.Notice that this string is modified by being broken into smaller strings (tokens). Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.

delimiters   C string containing the delimiter characters. These can be different from one call to another.

Example

/* strtok example */
 #include <stdio.h>
 #include <string.h>
int main (){
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL){
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}

code courtesy : cplusplus.com

Why Function strtok() is not Thread Safe / reentrant

The strtok() function uses a intermediate static buffer while parsing, and reuse them at each call so it’s not thread safe.

Solution :

Use  strtok_r() to use strtok as thread safe

 

Similar Articles

Filed Under: Adobe Interview Questions, problem Tagged With: c, string

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Apriori algorithm C Code Data Mining

Binary Tree Isomorphic to each other

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

Circular Linked List

Find if two rectangles overlap

Mirror of Tree

K’th Largest Element in BST when modification to BST is not allowed

Leetcode: Edit Distance

Cisco Hiring Event 21st – 22nd Feb 2015

Given a float number convert it into the string WITHOUT using any inbuilt Function

Minimum insertions to form a palindrome

Find Nearest Minimum number in left side in O(n)

Find loop in Singly linked list

Closed Parentheses checker

Stickler thief

The Magic HackerEarth Nirvana solutions Hiring Challenge

VMWare SDEII Interview

Hackerearth : Counting Subarrays

Memory Efficient LinkedList

Check Binary Tree is Binary Search Tree or not

CodeChef’ RRCOPY

Find the smallest window in a string containing all characters of another string

SAP Interview Questions

Find position of the only set bit

Handle duplicates in Binary Search Tree

Print all nodes that are at distance k from a leaf node

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Maximum sum contiguous subarray of an Array

Level order traversal in Spiral form

Printing Longest Common Subsequence

Copyright © 2026 · Genesis Framework · WordPress · Log in