• 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

Client Server C program

Possible sizes of bus to carry n groups of friends

Connect n ropes with minimum cost

Common Ancestor in a Binary Tree or Binary Search Tree

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

Implement LRU Cache

Test Cases for Round Function

Implement a generic binary search algorithm for Integer Double String etc

Stickler thief

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

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

Maximum difference between two elements s.t larger element appears after the smaller number

Hackerearth : Counting Subarrays

Naurki.com Security Breach

CodeChef Code SGARDEN

Singly linked list

Find the element that appears once others appears thrice

VMWare SDEII Interview

Practo Hiring Experience

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

Leetcode: Merge Intervals

Subset Sum Problem Dynamic programming

Calculate price of parking from parking start end time prices

1014 Practice Question of New GRE – Princeton

ADOBE Aptitude C Language Test

Stock Buy Sell to Maximize Profit

Printing Longest Common Subsequence

BlueStone E-commerce Interview Experience

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

Copyright © 2026 · Genesis Framework · WordPress · Log in