How to parse a string in C (sscanf)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we'll take a look at how to parse a string in see I have set up a few things here in order to get us started because we need a lot of preparation for this so first things first is the string that we want two pumps right so let's say this is actually in a file for example and what we have here is basically a sort of information about a person we have the person's name then we have its hometown let's say then we have its age and then we have if whether or not he has a bachelor's degree or not right that's basically boolean and to make this compatible with C I have treated bullying's as zeros and ones that's why it's a zero in this example right and then I have some variables here I just have the name and I initialize it all to zero city same thing then I have an unsigned int age because age cannot be the age cannot be a negative number and then we have them we have the boolean here as an int really but that can be 0 and 1 0 or 1 so there's no other value of value that it can have and then I'm just printing the information on the screen so if I try to run this right now I'm gonna get just blank right blank is in blank and it's 0 years old because well I've initialized them with nothing so what we have to do in here like in between this and this I want this data to be in each of the variables from over here right I want this endru to be in the name I want this New York to be in the city I want this 20 to be here and I want this 0 to be here how can we do this well you might remember that in the beginning you are told to read this number from the keyboard and what did you do usually what did you do well you would go ahead and say scanf % D and then the number let's say here I'm reading just an age let's say we're just going to be reading it from the keyboard if I try to run this you notice I can actually type in here now I can say let's say 19 I can hit enter and notice that we in in fact God that 19 inside our variable age right so that does work but it's only from the standard input it's only from the keyboard or is it as a matter of fact scanf is a family of functions in c that not only can work with the standard input with the keyboard itself it can also work with both strings and files and you can actually format them so what you can do here is start saying scanf we can use s scanf what that says is give me as the first parameter the string that you want parse as if it was already given to you from the keyboard and which string do i want to pass in here well actually want to pass in this input string right like I say STR here now what I have to do is specify a format write a format in which this data is aligned how is this data aligned well we can notice that first we have here a string then we have a semicolon then we have another string then a semicolon a number semicolon number right this means that we can use this semicolon as a sort of separator we can say here well for times of strings you know I'm gonna say percent s now say semicolon percent s because New York is a campus string then we have percent you because it's gonna be percent D because this is unsigned and I want it to be outside so I'm gonna just use percent you for unsigned integer and then there's a percent do-kyun because this guy isn't it and as with any other scanf that we do we have to adding here a variable for every single specifier right so we need one two three four variables in here in order so the first one is the name right let's make sense the second one is the city or rather a reference to the place you want to store the name and the city then the LA the third one is a reference to the preach one a store the edge I'm going to just pass in the reference to that age and same thing with the has degree variable do you agree just like so now if we try to run this you'll notice something very weird notice that we get Andrew semicolon nu is in blank and is zero years old so that's not right that's not quite right even though we have this string string integral integral setup it didn't read it right and when we try to print it in it as a matter of fact the first one here is the name and the name is actually Andrew semicolon ooh that's because % s actually reads everything up until a space or a tab or a backslash n so you cannot really use this percent as specified here you kind of have to say okay well I kind of want to read everything up until not a space but something else what you can do in this case is instead of saying percent as you can do something really neat that is implemented in basically any format specifier scanf you can say percent and you can open and close open close brackets and inside here you can specify whatever whatever characters you want to read right so I want to read basically everything except this semicolon I don't want to read this semicolon so I'm gonna say here a to z any lowercase letter and a to Z any uppercase letter that's in let's similar to reg X here and well maybe a space good yeah you could have two names in the earth that's not really a problem right same thing with this guy New York right we want to read the whole word up until the semicolon oh just knew right if I try to run this now you'll notice a pretty big difference you'll notice we get Andrew in the name so that's nice the name is correct now but New York well we don't get the whole thing we just get new and we also get zero years old why that's simple % s again reads up until the space here so we have to specify okay well read just every single letter and space included like I say again a to z a to z and a space and then % you and % d are going to be correct now because you'll notice we get here Andrew so has the correct name then we get the city which is New York with the space included so that works then we get 20 years old that's also correct which is really nice what happened before when we have % s well what happened is we would we read the name that was nice then we read the city knew then we read we kind of tossed the space and we were exactly here and here this guy the input corresponds with us with this % you and it couldn't pass that as a number right so that's why we didn't get anything here inside the age all right so you can do this you can use this notation or you can also use the inverse notation you can say read everything that is not doled out something so I can say read everything that is not a semicolon right because a semicolon is a separator and I want to read basically everything even if it's space I want to read it so I can change that to a not semicolon it would be the same exact thing in this case right we get the same exact result now you'll notice this sscanf function works with just strings but you can actually we can actually use this for reading from the keyboard just as we did before we've just connect so if we move this ass you're going to have to remove this fourth parameter and well if we try to run this now you'll notice the program kind of stopped so it's it's waiting for us to type in something here and we can say we can type in this thing again so I say Andrew semicolon New York let's say New York City this time and let's say 19 and let's say here one let's say he has a degree now and if we hit enter you notice we got the information inside these variables right so you can do the same with a string with with the standard input and in fact you can treat do the same with a file if use F scanf and then you would have to pass in the file handler the file handle right in here as the fourth parameter so this was how you can parse a string in C this basically works for most cases if you need to read a decimal point number you can if you want to read a boolean you can with zeros and ones not not so much with trend force if you have to read 24th gonna have to actually have probably a separate buffer and then compare the strings with actually true and false right as strings and then just set the boolean value if you want to and that's about it with any sort of format you can just read any string and parse it as needed hope this was useful fingers so much for watching if you have any questions do leave them down the comments below or on our discord server take care bye
Info
Channel: CodeVault
Views: 20,461
Rating: 4.9442511 out of 5
Keywords: c how to parse a string, parse string, c (programming language), scanf, sscanf, codevault, c programming
Id: -7cSmcdMryo
Channel Id: undefined
Length: 10min 38sec (638 seconds)
Published: Sun Jun 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.