Using strtok with Delimited Strings in C

Shibaji Paul
A free video tutorial from Shibaji Paul
Programming Instructor with 16+ years of experience
4.4 instructor rating • 7 courses • 26,317 students

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 the studio, we are going to talk about studio function that is one of the most useful functions. And see if you're on a street or can 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 adept 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 Andrieu and then a comma comes. So this comma is the delimiter which separates the 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, five point five. And then again, we had the comma for separating the weekly, which less four hundred dollars with this height. So we may have different kinds of delimiters in the string to separate information, but we are interested of extracting the individual information and that we can do using this strategy. OK, let's go ahead and see how we can do that. So I'll first declare another character, added Devlins, and that's going to be assigned with the delimiters that we are having there in the street. So it's actually comma and hash. You can declare these delimiters in any order. That doesn't matter. OK, so you can go and go on in any order. Now we are going to declare another character Pointer Dalkin that's going to that's going to have the best actress of each and individual tokens in that string. Now you need to extract each of these informations that's there in the source character added one after another using this data, OK. So this is how we can use the teoc. It will use in this way. Dalkin equals SDR, the OK. And the first parameter is the source stream from where you are going to extract that information. Now you're going to apply this year Diakite in succession for extracting the subsequent tokens. But for the first time, when you are applying that SDK, you need to mention the source stream. But for the second and subsequent time you need to mention only null if you are extracting from the same source stream. So you can see that I'm calling the static for the first time. So I'm supplying this source of string for the first time and then you need to supply the delimiters at it. OK, now it's going to actually extract this andrieu and that's going to be assigned. The best address of that is going to be assigned here. Let me go ahead and print that token so that names are just talking for some days and I'll just write it here. That's the best address. Let's go in and see if that extracts the name so I can see that and really step. Now, if the deal fails to find the token separated with the 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 this to educate again. So I'll just use token equals the OK. Now, this time, as I said earlier, you don't need to mention that sort of stream, but you need to mention null here and then that again. So for second and subsequent time, the source string should not be the only null is going to be there. Now let's go ahead and print the second token again, and it will be extracted as string, OK, 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 convert that to integer. So here we go. So is extracted, you can see that that fort is there. Now, if I apply this once more is going to extract the next one. That's this five point five. So you can see that is 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 before to write a loop there, I'll comment all those things. And as I said, if that fails to find a token, then it's going to return now. So we are going to iterate as long as that token is not null. So if that is not null, then we got a token there and we are printing that token into the console. Then we'll apply that stock once more to extract the next token. So here should be now. So now this while Loop is going to extract all the tokens from this source of string, let's go ahead and say. So I can see that all are extracted there in this way by this wide loop, so that's how this studio works. Thank you for watching.