Concise, yet a complete guide to using strtok. I never knew the function had a polluting effect on the original string buffer; I have never actually had need to look at the original string buffer again after a strtok() parse. Thank you!
@PortfolioCourses2 жыл бұрын
You're welcome Felix! :-)
@fifaham Жыл бұрын
I like to watch your videos and immediatly find practical use for them. Great video Kevin.
@PortfolioCourses Жыл бұрын
Thank you! :-)
@wheatdaddy_96292 жыл бұрын
Thanks for this video. I'm working on rebuilding some of the standard libraries in C, and this one was more difficult to find a good explanation on. Much appreciated!
@PortfolioCourses2 жыл бұрын
You're welcome! 😀 I'm glad to hear it was helpful.
@PortfolioCourses2 жыл бұрын
@@Trash7Trash7Trash7 hopefully this one helped! 🙂
@PortfolioCourses2 жыл бұрын
@@Trash7Trash7Trash7 You're welcome! 😀
@theworld-zr5jp Жыл бұрын
Became a big fan of your concept explanation sir, really appreciated, loved it..
@mllllll1211 Жыл бұрын
this is some really good step-by-step explanation
@PortfolioCourses Жыл бұрын
I’m glad you enjoyed it! :-)
@cchriz4292 Жыл бұрын
a short yet very detailed and helpful video!
@PortfolioCourses Жыл бұрын
I'm glad it was helpful for you! :-)
@HeavyMachinery5122 жыл бұрын
I was reading a book where I saw this strtok() function. But in the book it was not explained really well. so I turned up here and really have got basic understanding of it now. Brilliantly explained . keep up the good work!
@PortfolioCourses2 жыл бұрын
Thank you Muhammad! :-) I’m glad to hear the explanation was helpful!
@juliuskamara13482 жыл бұрын
@@PortfolioCourses This video is really amazing!
@Rain663 Жыл бұрын
Very well explained! I was wondering what that "NULL" was doing in the example code I'd found.
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed the explanation! :-)
@amanimavu8102 жыл бұрын
Thank you. Helped me know the problem in my code. That part of adding null bytes at the end of every section, is really important.
@PortfolioCourses2 жыл бұрын
You're welcome Amani! :-)
@kishorbabu9129 Жыл бұрын
Thank you for providing information in KZbin now a days KZbin became as knowledge hub ... thanks sir
@PortfolioCourses Жыл бұрын
You're welcome Kishor! :-)
@DataAnalyst-r6g Жыл бұрын
This is the best explanation of a function
@mastermax7777 Жыл бұрын
i really like the dive deep portion and thinking beyond just the basic functionality. starting at 9:00
@spring-tp6dr2 жыл бұрын
Very good explanation. Heaven send gift to C students!
@PortfolioCourses2 жыл бұрын
Thank you! :-D
@user-kv4gj9jp4h Жыл бұрын
You explain everything so well thanks
@PortfolioCourses Жыл бұрын
You're welcome, I'm glad you enjoy the explanations! :-)
@Ajamtroye8 ай бұрын
Ok i made a strdup to a an other strings but i don't find how can i then free the string
@lordmortymer2 жыл бұрын
Awesome explanation. Will be avoiding this function as much as possible now lol
@PortfolioCourses2 жыл бұрын
I'm glad you enjoyed the explanation! 🙂
@aryamansrivastava1816 Жыл бұрын
amazing videoo..cleared the entire concept to me
@LapCreativeStudio3 жыл бұрын
sir thank u so much for your explanation thank u love your efforts
@PortfolioCourses3 жыл бұрын
Thanks so much for sharing this, that’s awesome to hear you enjoy the video! :-)
@normanweiz26462 жыл бұрын
Thanks for your great explanation. That was very clear and easy to follow. Well done. I have always had difficulty understanding strtok. I had a thought while watching your explanation. What if you had some numbers in the string and you wanted to retrieve each number and assign to separate variables? Could that be done using the while loop or is it better to do that manually as you did using the *portion?
@PortfolioCourses2 жыл бұрын
Thank you Norman! And it sounds like that could be done using a while loop, which would be useful if you don’t know how many numbers there are in the string. But I think either approach could work ok too!
@Henry-sv3wv Жыл бұрын
if you want to assign into an int then you still need to convert the string: char str[]="12"; int val = atoi(str);
@zhaoaverie2256 Жыл бұрын
Thank you so much for the awesome tutorial!
@PortfolioCourses Жыл бұрын
You’re very welcome Zhao, I’m glad you enjoyed it! :-)
@PasanKarunanayake Жыл бұрын
Superb explanation.👍
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed it Pasan! :-)
@amanwehib83672 жыл бұрын
Great explanation! Thank you very much.
@PortfolioCourses2 жыл бұрын
You're welcome Aman! :-)
@SlideRSB10 ай бұрын
Why did you end up with two null terminators at the end after the tokenization?
@davidverheyen663511 ай бұрын
Thank you, chars confuse me less and less. What would be the best way to use a delimiter that is 'unprintable', like STX (ascii value 2).
@decibi2684 Жыл бұрын
Hi Sir, Could you please make video on how to implement our own strtok function ? Thanks in Advance.
@azarelgrahanditoadi75072 жыл бұрын
thankss for the explanation! this is the way.
@PortfolioCourses2 жыл бұрын
You're very welcome Azarel! :-)
@neilanpadilla4 ай бұрын
just wanna ask, do you guys use this ' * ' for memory allocation?
@jess-inspired2 жыл бұрын
Hello sir. Read that strings in c cannot be modified yet strtok modifies the original string. Was thinking it would return a pointer to a new string on the heap. So please can you explain how this truly works under the hood?
@PortfolioCourses2 жыл бұрын
Strings in C *can* be modified, so that's essentially how it's working, strtok() *is* modifying the string. 🙂
@jess-inspired2 жыл бұрын
@@PortfolioCourses Thanks a lot sir.. Let me show you a code I've been having issues with lately.. Code 1: works well..' a' is modified to 'k' int main(void) { char str[] = "abcd"; str[0] = 'k'; printf("%c ", str[0]); return 0 } Code 2: segmentation fault int main(void) { char *str = "abcd"; str[0] = 'k'; printf("%c ", str[0]); return 0 }
@jess-inspired2 жыл бұрын
@@PortfolioCourses Not really sure, why the second isn't working..
@PortfolioCourses2 жыл бұрын
@@jess-inspired Oh that's a tricky one, basically in the 1st example a char array is created to store the string. In the second example, s becomes a pointer to a 'string literal' stored in a read-only part of memory where modifying them is not allowed by most compilers (the C language definition leaves the exact behaviour as 'undefined'). So if you want to create and initialize a string that you want to be able to modify, you need to do it the first way. The second way does create a string that is a sequence of chars ending with a null terminator '\0', BUT it will be 'read-only' in that case. And you know what? This is a FANTASTIC idea for a video haha... I think I might make this video soon explaining this difference because it's definitely confusing! 😀
@jess-inspired2 жыл бұрын
@@PortfolioCourses Wow! That would be great. Encountered this issue while trying to write my own strtok function.. Thanks a lot sir, it's an honour..
@dimitriossomarakis39462 жыл бұрын
what if the message is numbers, for example:"22-33-22-44-63-66" and I want to take a couple of them every time. how can I proccess the numbers after that. for example to push 22 into a variable that is type integer??
@PortfolioCourses2 жыл бұрын
Great question Dimitrious! You could use atoi() to convert the string into an integer: cplusplus.com/reference/cstdlib/atoi/. I have a video on that one: kzbin.info/www/bejne/i4KynYeadtd2iNE. :-)
@dimitriossomarakis39462 жыл бұрын
@@PortfolioCourses I think I also can do that with strtok function in order to keep the 2 digits and after that I can use sscanf to convert it into an intiger. What do you think? Thanks for responding!
@PortfolioCourses2 жыл бұрын
@@dimitriossomarakis3946 Yes, I suspect sscanf may work as well. You may even be able to just use sscanf from the start and not even use strtok() at all. Maybe try a format string like "%d-%d-%d-%d-%d-%d" to read in 6 ints separated by - chars? 🙂
@dimitriossomarakis39462 жыл бұрын
@@PortfolioCourses yeah I will try , thanks man
@silentcal2758 ай бұрын
What is in your string.h and studio.h files?
@mastermax7777 Жыл бұрын
a question, how is it keeping track of how far its in the string? what if i call another strok. what if i call it again using s instead of NULL?
@lukeherbst7931 Жыл бұрын
If you call it again using S, it will start over from the beginning
@1kvolt19784 ай бұрын
Probably, it just saves the last used pointer in static variable.
@notebook28662 жыл бұрын
best tutorial ever
@PortfolioCourses2 жыл бұрын
Thank you very much for the positive feedback! :-)
@sasindudilshan66792 жыл бұрын
thank you very much sir !
@PortfolioCourses2 жыл бұрын
You're very welcome Sasindu! :-)
@dimkayilrit2606 Жыл бұрын
Great content
@PortfolioCourses Жыл бұрын
I’m glad you enjoyed the video Dimka! :-)
@happytime30422 жыл бұрын
Thank you Sir 😊
@PortfolioCourses2 жыл бұрын
You’re welcome! :-)
@fifaham Жыл бұрын
This code works to extract the password, and others, inputed from streaming data of mobile phone: char s1[30] = "@P123456@D254@X232"; char d1[4] = "@P"; // Password 123456 char d2[4] = "@D"; // Deactivate 254 char d3[4] = "@X"; // Activate 323 char *portion1 = strtok(s1, d1); printf("The password is: %s ", portion1); // works nice I will try to implement in my project. Thank you Kevin.
@PortfolioCourses Жыл бұрын
Thank you for sharing this! :-)
@codingprogramming95102 жыл бұрын
Thank you sir for this amazing channel I really benefit from it a lot! keep the good work going! Sir i have a question a bout this command line: (inside the while loop) portion = strtok(NULL, d); Q1: what exactly happen when the loop reach this line? I didn't understand very well how this line work... if it first argument is NULL why the loop keep going? shouldn't it stop after the first delimiter it face, because this delimiter will be NULL, I don't know if you understand what I meant, but it will be helpful if you can explain to me what happen in that command line, I will really appreciate it. [i am still learning :) C language ] Thank you.
@PortfolioCourses2 жыл бұрын
I'm glad to hear you enjoy the channel! :-D So the strtok() function is a bit special, because we call it multiple times for each "portion" of the string, and we call it differently the first time compared to the subsequent calls (for the same string). So the FIRST time we call strtok() above the loop we *do* give it the string s as an argument (and delimiters are the 2nd argument d): char *portion = strtok(s, d); And then inside the loop, in the subsequent calls, instead of s we call it with NULL as the first argument: portion = strtok(NULL, d); This is a signal that tells strtok() to "keep using the same string that we previously passed in". So even though we are passing in NULL, strtok() will continue to use the string s from the previous call. And it will try to find the *next* delimiter in the string from it's previous position (where d is the string of delimiters). "On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of the last token as the new starting location for scanning." ^ that is from this documentation of the function here: www.cplusplus.com/reference/cstring/strtok/
@codingprogramming95102 жыл бұрын
@@PortfolioCourses Thanks a lot Sir, now I understand it well. Really appreciate it.
@PortfolioCourses2 жыл бұрын
You’re welcome! :-)
@ShubhashChandraYadav-e2w Жыл бұрын
Thank you sir
@nerd61343 жыл бұрын
Nice 👍
@PortfolioCourses3 жыл бұрын
Thank you! :-D
@cryptotechcoder Жыл бұрын
Very awsome
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed it! :-)
@cryptotechcoder Жыл бұрын
@@PortfolioCourses I'm glad you made it
@krish-ut9de3 ай бұрын
Thank you very much sir
@Ak-zm3ce3 жыл бұрын
great
@dwadwadw87302 жыл бұрын
thanks
@PortfolioCourses2 жыл бұрын
You're welcome! :-D
@dwadwadw87302 жыл бұрын
@@PortfolioCourses can you print all the delimited strings in a new matrix and then sort them aphabetically
@PortfolioCourses2 жыл бұрын
@@dwadwadw8730 With some dynamic memory allocation, string copying, and a sorting algorithm, that's definitely possible. :-)
@abdurraheem24442 жыл бұрын
best
@PortfolioCourses2 жыл бұрын
Thank you Abdur! :-)
@ryanalnaser9142 жыл бұрын
whatever anything
@PortfolioCourses2 жыл бұрын
I don't know if this is a question or not. :-)
@stathislourantos1194 Жыл бұрын
Very good educative video. One question though: How would I split a text using multiple delimiters? Let's say, taking your example, how would I get rid of the final dot after the word "way"? Or, if the text was: "This_is: the way! folks...", how would I split the text ending up with only the words? This is the way folks
@1kvolt19784 ай бұрын
Write your own function obviously. Because this is exactly how it works in programming.