How to split strings in C (strtok)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I want to talk a bit about how you can manipulate strings in c namely how you can actually split a string in multiple pieces so so let's get started I have declared here and initialized a simple string right we have STR that is the text let's break this string into pieces and I want to get every single word out on a different line on the console simple enough okay so to start off we need to first include a string that age so I have included here single age and we're going to use a function from this library so the name of the function is STR Tok and that stands for string token we have to pass in as a first parameter our string to this function so that's just going to be STR and then the second parameter is going to be a string that contains each character that we want to break at so we just want to print out every single word on the console so we are just going to pass in a string that contains a space just like so and that's it that's the first phase of splitting the string now this function also returns a char pointer right and it will return a pointer to the next occurrence of each piece of our whole string right so we're going to just simply declare it here so chart pointer I'm going to call it piece because the piece of our string so the first time we call STR talk it's going to return the first part the first piece of our string right so now we can actually print this on the screen so you can say print F and then % s backslash N and simply say piece here and if we try to run is you'll notice that we get well just the first world which is let's so that makes sense now how we can actually print every single work there well for that we're going to have to use STR talk yeah but now in a different way now that we have given it the string it already knows even after it finished its execution it it still retains a bit of its memory right it knows that we're still dealing with this this string this STR here so for the next pieces of our string what we have to do is call STR talk with the same second parameter right so the same delimiter but now with the first parameter being no and that tells the function - oh we're not gonna pass you any more new strings just use the previous one and continue giving us pieces of that string right and we're gonna still assign this value to piece so this is still gonna give us the next occurrence the next piece of our string and now if we try to print this you will notice that we get the second piece of our string now there's one important thing to note here when using STR talk and to get to it we're gonna have to use the debugger so let's get straight to it I'm just going to end a breakpoint here we're gonna launch the debugger and let's see what value STR has so right now it has the actual string that we've added here that's fair enough we haven't executed anything yet now let's try to execute this line of code right so I'm gonna hit f7 and now if I evaluate this you'll notice if we get this let's back search zero zero zero break this string into pieces from this we can deduce that one STR talk is actually modifying our original string so if we want to use it further down we're gonna have to copy it somewhere else but second that also means that these piece parts are actually part of the string they are actually pointing to the same string just at different places and due to it being zero when we try to print it here if we if we take a look at peace here you'll notice that it's 0 X - 3 F each one and you'll notice that STR has the exact same address location you'll notice that both of them are pointing to this exact place in memory and that's right because that's where our first piece of the string is while splitting it after the space character and that's where our string starts so that's correct so that's one thing to note here the we're actually dealing with the same place in memory we actually are working with our string it's not just a copy of the word lats as the important thing to note here and now if we go further down so printing it doesn't do anything to the STR if I try to run this you notice that we don't get anything different but if I try to run this again you'll notice that it added another backslash 0 at the end that's actually the character backslash zero checked that's how the debugger shows a null terminator in the string here so that's just a just the moon terminator so basically it simply adds a null terminator to the end of our piece and gives us back a reference to the beginning of our piece right so now we can simply we can simply print it on the screen just like so and we get our word here now one thing to note here is this STR talk how do we know that we've reached the end how do we know that ok here's the last piece and there's no more no nothing to give you because if we always get a reference then we can't know that well as the attack has a feature where it says ok once you've reached the end of the string itself and you already gave us the last piece simply return null right so we can check for that in fact we can simply change this whole code into a while loop so it's gonna go like so we simply have a while while piece is different than the point or null simply print on the screen the piece and then simply give me the next piece so now if we try to execute this you'll notice that we get the right output that we wanted just every single word on a different line now the cool thing about STR talk is that it also breaks at multiple consecutive occurrences of the same delimiter what I mean by that is just simply it breaks at multiple spaced characters and when we try to run this you'll notice that we don't get simply empty lines here so that means that instead of just seeing a single space is so three spaces and split at that some other languages have this problem where when you break at the the limited space it's going to give us two empty strings in this case but for C for STR talk at least it's not a case so as a recap it's actually very simple to understand once you get the gist of how it actually works so first you need to call STR talk with your own string that you want to split just once right and the delimiter of course and retain whatever you get back inside a pointer inside a char pointer that's gonna give you the first piece now you can simply call STR talk with the null pointer and the same the limiters multiple times to get the next piece over and over and over again and you have to just remember that at one point you're gonna get the null pointer right this guy is going to simply return null and your piece is going to be simply noil and it's why we have this condition as a stop condition for the while loop right so these are the three parts that you have to remember just start the splitting get the next piece and stop the splitting add the null pointer so I hope this helped feel free to check out more videos about strings in C because I know it's a little bit of a confusing chapter when first learning C and well see you next time
Info
Channel: CodeVault
Views: 53,675
Rating: 4.9575033 out of 5
Keywords: split strings, split strings in c, strtok in c, strtok, how to use strtok, how to split strings, how to split strings in c, codevault, splitting strings, string token, split string
Id: 34DnZ2ewyZo
Channel Id: undefined
Length: 9min 27sec (567 seconds)
Published: Thu Jan 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.