• 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

Find min element in Sorted Rotated Array (Without Duplicates)

Printing intermediate Integers between one element & next element of array

Daughter’s Age VeryGood Puzzle

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 if an array has duplicate numbers in O(n) time and O(1) space

Diagonal Traversal of Binary Tree

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

Maximum path sum between two leaves

Cisco Hiring Event 21st – 22nd Feb 2015

Longest Increasing Subsequence

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

Leetcode: Edit Distance

FizzBuzz Solution C C++

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

VMWare Openings

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

Subset Sum Problem Dynamic programming

Check Binary Tree is Binary Search Tree or not

Urban Ladder Written Test.

Maximum sum contiguous subarray of an Array

Hackerearth : Counting Subarrays

Generate next palindrome number

Stock Buy Sell to Maximize Profit

C++ OOPs Part1

C Program for TAIL command of UNIX

Skiing on Mountains Matrix

SAP Off Campus Hiring_ March 2015 Verbal Skills

Amazon Interview Experience – SDE Chennai

Circular Linked List

Closed Parentheses checker

Copyright © 2025 · Genesis Framework · WordPress · Log in