Using strtok with Delimited Strings in C

A free video tutorial from Shibaji Paul
Programming Instructor with 16+ years of experience
Rating: 4.4 out of 5Instructor rating
7 courses
32,818 students
strtok - great tool to extract information from a string separated by delimiters

Lecture description

How you can separate information from a string which are delimited by different characters, you can do it in a sec using strtok.

Learn more from the full course

C Programming Step by Step - Complete Tutorial For Beginners

C language tutorial from basics with C operator,loop,array,pointer,function,parameter,string,recursion,structure,file.

17:18:44 of on-demand video • Updated June 2020

if else statements, loop - while, for and do while loop with many examples.
Array - 1D and 2D, why we need them and how to use them effectively.
String in C - NULL terminated character arrays.
Writing function, parameter passing to function. Returning value from function.
storage class - auto, static, extern and register
Pointer - in depth understanding.
Relationship between arrays and pointers.
Array of pointers.
Command line arguments
Reading and writing with files, both text and binary.
Recursion - how it works, recursion vs iteration in depth discussion - Towers of Hanoi
Various string utilities - sprintf, strtok and many others
Function pointers
bitwise operators in C programming.
English [Auto]
Hey, welcome back. Now in this tutorial we are going to talk about STR function that is one of the most useful functions in C str or string token function can be used for extracting the useful information from a string which are separated by some delimiters. As you can see in this example, I already have declared a character array at line number seven named source and that is initialized with this string. Now this string contains some useful information for the program, and the first one is the name of the person, and that is actually Andrew Yule. And then a comma comes. So this comma is the delimiter, which separates two information in a string. Now we are having more than one delimiters in this example. As you can see that this comma is there to separate this name with this age. And there is another kind of delimiter that is hash and that is actually separating this age with the height of the person, 5.5. And then again, we have the comma for separating the weekly wage. That's $400 with this height. So you may have different kinds of delimiters in the string to separate the information, but we are interested of extracting the individual information and that we can do using this stock. Let's go ahead and see how we can do that. So I'll first declare another character array, Dellums and that's going to be assigned with the delimiters that we are having there in the string. So it's actually comma and hash. You can declare these delimiters in any order that doesn't matter. Okay, so you can go and go on in any order. Now we are going to declare another character pointer token. That's going to that's going to have the base address of each and individual tokens in that string. Now you need to extract each of these informations that's there in the source character array one after another using the stock. So this is how we can use the stock we will use in this way. Token equals STR stock and the first parameter is the source string from where you are going to extract the information. Now we are going to apply this stock in succession for extracting the subsequent tokens, but for the first time when you are applying the stock, you need to mention the source string. But for the second and subsequent time you need to mention only null if you are extracting from the same source string. So you can see that I'm calling the stock for the first time, so I'm supplying this source string for the first time and then you need to supply the delimiters array. Okay, Now it's going to actually extract this entry and that's going to be assigned the base address of that is going to be assigned here. Let me go ahead and print that token. So the name or just token percentages and I'll just write token here. That's the base address. Let's go ahead and see if that extracts the name so you can see that and you is there. Now if the stock fails to find the token separated with a given delimiters, then it's going to return null. Now, if you want to extract the second information that is this 40, then you need to apply the stock again. So I'll just use token equals stock. Now, this time, as I said earlier, you don't need to mention that source string, but you need to mention null here and then the dell-emc again. So for second and subsequent time, the source string should not be there, only Null is going to be there. Now let's go ahead and print the second token again. Now it will be extracted as string. Okay. Although this is a integer format data. Now if you want it to be converted to integer, you can apply it to a function that's going to take this token and it will convert that to integer. So here we go. So it is extracted. You can see that that 40 is there. Now, if I apply this once more, it's going to extract the next one. That's this 5.5. So you can see that it's happening. So if you are going to extract all of those information and then it will be better if you just write a loop. So instead of writing in this way, I will just prefer to write a loop there. I will comment all these things. And as I said, if the stock fails to find a token, then it's going to return null. So we are going to iterate as long as the token is not null. So if the token is not null, then we got a token there and we are printing that token into the console. Then we will apply the stock once more to extract the next token. So here it should be null. So now this while loop is going to extract all the tokens from this source string. Let's go ahead and see. So I can see that all are extracted there in this while loop by this. While loop. So that's how this works. Thank you for watching.