strtok() function | C Programming Tutorial

  Рет қаралды 58,355

Portfolio Courses

Portfolio Courses

3 жыл бұрын

An overview of how to use strtok() function in C. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 100
@theworld-zr5jp
@theworld-zr5jp 7 ай бұрын
Became a big fan of your concept explanation sir, really appreciated, loved it..
@fifaham
@fifaham 11 ай бұрын
I like to watch your videos and immediatly find practical use for them. Great video Kevin.
@PortfolioCourses
@PortfolioCourses 11 ай бұрын
Thank you! :-)
@felixobianozie3745
@felixobianozie3745 2 жыл бұрын
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!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome Felix! :-)
@cchriz4292
@cchriz4292 Жыл бұрын
a short yet very detailed and helpful video!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad it was helpful for you! :-)
@user-mq3bn4kx9h
@user-mq3bn4kx9h Жыл бұрын
This is the best explanation of a function
@amanimavu810
@amanimavu810 Жыл бұрын
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.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome Amani! :-)
@sainithin414
@sainithin414 Жыл бұрын
Extremely good, thanks for your great video. Keep going .
@wheatdaddy_9629
@wheatdaddy_9629 Жыл бұрын
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!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! 😀 I'm glad to hear it was helpful.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
​@@Trash7Trash7Trash7 hopefully this one helped! 🙂
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@Trash7Trash7Trash7 You're welcome! 😀
@user-kv4gj9jp4h
@user-kv4gj9jp4h Жыл бұрын
You explain everything so well thanks
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome, I'm glad you enjoy the explanations! :-)
@zhaoaverie2256
@zhaoaverie2256 Жыл бұрын
Thank you so much for the awesome tutorial!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re very welcome Zhao, I’m glad you enjoyed it! :-)
@aryamansrivastava1816
@aryamansrivastava1816 9 ай бұрын
amazing videoo..cleared the entire concept to me
@mllllll1211
@mllllll1211 8 ай бұрын
this is some really good step-by-step explanation
@PortfolioCourses
@PortfolioCourses 8 ай бұрын
I’m glad you enjoyed it! :-)
@amanwehib8367
@amanwehib8367 Жыл бұрын
Great explanation! Thank you very much.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome Aman! :-)
@lordmortymer
@lordmortymer Жыл бұрын
Awesome explanation. Will be avoiding this function as much as possible now lol
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed the explanation! 🙂
@spring-tp6dr
@spring-tp6dr 2 жыл бұрын
Very good explanation. Heaven send gift to C students!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you! :-D
@mastermax7777
@mastermax7777 8 ай бұрын
i really like the dive deep portion and thinking beyond just the basic functionality. starting at 9:00
@kishorbabu9129
@kishorbabu9129 Жыл бұрын
Thank you for providing information in KZfaq now a days KZfaq became as knowledge hub ... thanks sir
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome Kishor! :-)
@Rain663
@Rain663 Жыл бұрын
Very well explained! I was wondering what that "NULL" was doing in the example code I'd found.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed the explanation! :-)
@LapCreativeStudio
@LapCreativeStudio 2 жыл бұрын
sir thank u so much for your explanation thank u love your efforts
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thanks so much for sharing this, that’s awesome to hear you enjoy the video! :-)
@azarelgrahanditoadi7507
@azarelgrahanditoadi7507 Жыл бұрын
thankss for the explanation! this is the way.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're very welcome Azarel! :-)
@user-bh6ii2ns5x
@user-bh6ii2ns5x 10 ай бұрын
Thank you sir
@PasanKarunanayake
@PasanKarunanayake Жыл бұрын
Superb explanation.👍
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed it Pasan! :-)
@HeavyMachinery512
@HeavyMachinery512 2 жыл бұрын
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!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you Muhammad! :-) I’m glad to hear the explanation was helpful!
@juliuskamara1348
@juliuskamara1348 Жыл бұрын
@@PortfolioCourses This video is really amazing!
@dimkayilrit2606
@dimkayilrit2606 Жыл бұрын
Great content
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I’m glad you enjoyed the video Dimka! :-)
@sasindudilshan6679
@sasindudilshan6679 Жыл бұрын
thank you very much sir !
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're very welcome Sasindu! :-)
@davidverheyen6635
@davidverheyen6635 5 ай бұрын
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).
@happytime3042
@happytime3042 2 жыл бұрын
Thank you Sir 😊
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome! :-)
@SlideRSB
@SlideRSB 4 ай бұрын
Why did you end up with two null terminators at the end after the tokenization?
@Ajamtroye
@Ajamtroye 3 ай бұрын
Ok i made a strdup to a an other strings but i don't find how can i then free the string
@decibi2684
@decibi2684 Жыл бұрын
Hi Sir, Could you please make video on how to implement our own strtok function ? Thanks in Advance.
@normanweiz2646
@normanweiz2646 2 жыл бұрын
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?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
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
@Henry-sv3wv 11 ай бұрын
if you want to assign into an int then you still need to convert the string: char str[]="12"; int val = atoi(str);
@silentcal275
@silentcal275 2 ай бұрын
What is in your string.h and studio.h files?
@notebook2866
@notebook2866 Жыл бұрын
best tutorial ever
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you very much for the positive feedback! :-)
@cryptotechcoder
@cryptotechcoder 11 ай бұрын
Very awsome
@PortfolioCourses
@PortfolioCourses 11 ай бұрын
I'm glad you enjoyed it! :-)
@cryptotechcoder
@cryptotechcoder 11 ай бұрын
@@PortfolioCourses I'm glad you made it
@nerd6134
@nerd6134 2 жыл бұрын
Nice 👍
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you! :-D
@ritapiu319
@ritapiu319 8 ай бұрын
THANKS
@PortfolioCourses
@PortfolioCourses 7 ай бұрын
You’re welcome! :-)
@Ak-zm3ce
@Ak-zm3ce 2 жыл бұрын
great
@mastermax7777
@mastermax7777 8 ай бұрын
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
@lukeherbst7931 8 ай бұрын
If you call it again using S, it will start over from the beginning
@Local_Nerd
@Local_Nerd Жыл бұрын
Thanks
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! :-)
@jess-inspired
@jess-inspired 2 жыл бұрын
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?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Strings in C *can* be modified, so that's essentially how it's working, strtok() *is* modifying the string. 🙂
@jess-inspired
@jess-inspired 2 жыл бұрын
@@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-inspired
@jess-inspired 2 жыл бұрын
@@PortfolioCourses Not really sure, why the second isn't working..
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@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-inspired
@jess-inspired 2 жыл бұрын
@@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..
@dimitriossomarakis3946
@dimitriossomarakis3946 Жыл бұрын
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??
@PortfolioCourses
@PortfolioCourses Жыл бұрын
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: kzfaq.info/get/bejne/i7d_n7eVptingqM.html. :-)
@dimitriossomarakis3946
@dimitriossomarakis3946 Жыл бұрын
@@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!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@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? 🙂
@dimitriossomarakis3946
@dimitriossomarakis3946 Жыл бұрын
@@PortfolioCourses yeah I will try , thanks man
@codingprogramming9510
@codingprogramming9510 2 жыл бұрын
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.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
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/
@codingprogramming9510
@codingprogramming9510 2 жыл бұрын
@@PortfolioCourses Thanks a lot Sir, now I understand it well. Really appreciate it.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome! :-)
@dwadwadw8730
@dwadwadw8730 2 жыл бұрын
thanks
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome! :-D
@dwadwadw8730
@dwadwadw8730 2 жыл бұрын
@@PortfolioCourses can you print all the delimited strings in a new matrix and then sort them aphabetically
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@dwadwadw8730 With some dynamic memory allocation, string copying, and a sorting algorithm, that's definitely possible. :-)
@abdurraheem2444
@abdurraheem2444 2 жыл бұрын
best
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you Abdur! :-)
@ryanalnaser914
@ryanalnaser914 2 жыл бұрын
whatever anything
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I don't know if this is a question or not. :-)
@erenyeager45god
@erenyeager45god 7 ай бұрын
thanks ❤
@fifaham
@fifaham 11 ай бұрын
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
@PortfolioCourses 11 ай бұрын
Thank you for sharing this! :-)
@stathislourantos1194
@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
strcspn() function | C Programming Tutorial
3:10
Portfolio Courses
Рет қаралды 9 М.
Survival skills: A great idea with duct tape #survival #lifehacks #camping
00:27
Heartwarming: Stranger Saves Puppy from Hot Car #shorts
00:22
Fabiosa Best Lifehacks
Рет қаралды 21 МЛН
THEY WANTED TO TAKE ALL HIS GOODIES 🍫🥤🍟😂
00:17
OKUNJATA
Рет қаралды 21 МЛН
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
How to split strings in C (strtok)
9:28
CodeVault
Рет қаралды 106 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,1 МЛН
Scanf Basics: the good, the bad, and why so many pointers?
15:07
Jacob Sorber
Рет қаралды 22 М.
why is it illegal to use "goto"?
5:23
Low Level Learning
Рет қаралды 245 М.
Intro to Competitive Programming
11:41
Junferno
Рет қаралды 765 М.
Dynamically Allocate An Array Of Structs | C Programming Tutorial
15:11
Portfolio Courses
Рет қаралды 33 М.
Dear Functional Bros
16:50
CodeAesthetic
Рет қаралды 474 М.
Look at two different videos 😁 @karina-kola
0:12
Andrey Grechka
Рет қаралды 4,2 МЛН
Порезал бассейн, краш тест
0:32
ЗВЕРЬЕ
Рет қаралды 3,9 МЛН
УТОПИЛА ДОРОГУЩИЙ ТЕЛЕФОН 😭
1:01
Аня Панкова
Рет қаралды 5 МЛН
Как красиво снять полет #marim #shortsvideo
0:15
Всегда проверяйте зеркала
0:21
Up Your Brains
Рет қаралды 4,7 МЛН