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 • 20,877 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.

16:57:20 of on-demand video • Updated November 2019

  • 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 we'll come back to you. We are going to talk about SDR do function. That is one of the most useful functions in C or D ok or a strength or can function can be used for extracting the useful information from a stream which is separated by some kilometers as it can see in this example I already have declared a character at a at line number seven named source and that is initialized with this string. Now this last drink contains some useful information for the program. And the first one is the name of the person and that is actually the entry you 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 daily meters. In this example as it can see that this Alma 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 fight but fight. And then again we had 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 information but we are into a state of extracting that individual information and that we can do using this SGMD OK. Let's go ahead and see how we can do that. So I'll first declare an other character added Dellums. That's going to be signed with the daily meters that we are having there in the street. So it's actually called mind 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 talken that's going to do that would have the best actress off each individual tokens in that history. Now you need to extract each of these informations that they're in the source character at one after another using this. OK. So this is how we can use that as Teotihuacan will use in this way. Talken the quolls SDR. OK. And the first bad teacher is the source string from where you're going to extract the information. Now you're going to apply this as Dear D ok in succession for extracting the subsequent tokens. But for the first time when you are applying the SGA to your. You need to mention the source string but for the second and subsequent time you need to mention only now if you are extracting from the same source string so you can see that I'm calling this DFT OK for the first time so I'm supplying this sort of string for the first time and then just supply the daily meters at it. OK. Now it's going to actually extract this and to you and that's going to be assigned the address of that is could be assigned here. Let me go ahead and print that took in. So then they are just talk in recent days and I'll just write it here. That's the best actress. Let's go in and see if that extracts the name so I can see that. And viewless there. Now if that is yet the UK fails to find that you can separate it with the given delimiters then it's going to return now. Now if you want to extract the second information that is this 40 then you need to apply this to it again. So I'll just use talking equals to. At this time. As I said earlier you don't need to mention that social stream but you need to mention knowledge here and then that leaves again. So for second and subsequent time the source string should not be there only now is going to be there. Now let's go ahead and print the second in again and it will be extracted as straight. OK. Although this is editio format data. Now if you want it to be converted to the church you can apply it to I function. That's when to take stock in and who can forget to integer. So here we go. So it is extracted you could see that that 40 is there. And if I applied 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'll comment all these things. And as I say if the UK fails to find a token then it's going to return now. So we are going to iterate as long as that talken is not now. So either it's not now then we got talking there and we're printing that token into the console then apply the STF OK. Once more to extract the mix talking. So here should be now. So now this why loop is going to extract all the tokens from the source Stream. Let's go ahead and see. So I can see that all are extracted there in this Why by this why. So that's how this works. Thank you for watching.