• 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

Generate largest number arranging a no. of given non negative integer numbers

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

Calculate price of parking from parking start end time prices

Trapping Rain Water

Convert number to words java

Sort an array according to the order defined by another array

Printing each word reverse in string

Walmart Labs Interview Experience

SAP Hiring Off-Campus General Aptitude

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

Flipkart SDET Interview Experience

Mirror of Tree

Closed Parentheses checker

SAP Off Campus Hiring_ March 2015 Sample Questions

Stickler thief

Get K Max and Delete K Max in stream of incoming integers

SAP Off Campus Hiring_ March 2015 Verbal Skills

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Knight Tour Problem (Graph – Breadth First Search)

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

Printing Longest Common Subsequence

Regular Expression Matching

Doubly linked list

Find position of the only set bit

Maximum size of square sub matrix with all 1’s in a binary matrix

Word Break Problem

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 ?

ADOBE Aptitude C Language Test

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

Copyright © 2026 · Genesis Framework · WordPress · Log in