• 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

Level order traversal in Spiral form

Password Predictor

Leetcode: Edit Distance

Maximum of all subarrays of size k

Cisco Hiring Event 21st – 22nd Feb 2015

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

BFS (Breath First Search)

Maximum path sum between two leaves

Urban Ladder Written Test.

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

C Program for TAIL command of UNIX

TicTacToe Game As Asked in Flipkart

Find the element that appears once others appears thrice

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

Binary Tree Isomorphic to each other

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

Trapping Rain Water

LeetCode : Word Search

Find next greater number with same set of digits

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Find the kth number with prime factors 3, 5 and 7

SAP Off Campus Hiring_ March 2015 Verbal Skills

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

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 ?

Code Chef PRGIFT Solution

Find Percentage of Words matching in Two Strings

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

The greedy coins game Dynamic Programming

Print vertical sum of all the axis in the given binary tree

Copyright © 2026 · Genesis Framework · WordPress · Log in