• 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

Linked List V/S Binary Search Tree

Skiing on Mountains Matrix

Length of the longest substring without repeating characters

Connect n ropes with minimum cost

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

Flipkart SDET Interview Experience

Facebook Interview Question : Interleave List

Find Percentage of Words matching in Two Strings

Count number of ways to reach a given score in a game

Mirror of Tree

Get Minimum element in O(1) from input numbers or Stack

Find position of the only set bit

Code Chef PRGIFT Solution

Cisco Hiring Event 21st – 22nd Feb 2015

Implement LRU Cache

Daughter’s Age VeryGood Puzzle

Flipkart Set 1 On Campus with Answers

Wrong Directions given find minimum moves so that he can reach to the destination

C++ OOPs Part1

Maximum sum contiguous subarray of an Array

ADOBE Aptitude C Language Test

Right view of Binary tree

Trapping Rain Water

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Find Pythagorean Triplets in an array in O(N)

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

Find min element in Sorted Rotated Array (Without Duplicates)

CodeChef Code SGARDEN

Printing Longest Common Subsequence

Naurki.com Security Breach

Copyright © 2026 · Genesis Framework · WordPress · Log in