Split C strings into tokens with strtok.

preview_player
Показать описание
---

Split C strings into tokens with strtok. // Want to know how to split strings into tokens in C? A lot of my students reach their third year of college without seeing strtok. This video is an attempt to fix that.

Also, there are two other options: strtok_r and strsep that are discussed in another video. Link below.



***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:

To Support the Channel:
+ like, subscribe, spread the word

Want me to review your code?

You can also find more info about code reviews here.
Рекомендации по теме
Комментарии
Автор

I was aware of strtok and used it once in passing, but I never knew it was destructive or not safe for threaded programs. I guess I got lucky that one time, haha. Good video!

BABABOOWIE
Автор

Good vid. I used strtok to parse a csv file and output it to a table in markdown syntax. Very handy!

adammontgomery
Автор

Thanks Alot sir I'll keep watching your videos to learn every thing that new to me I didn't know about this function as well thanks again

JustinZaf
Автор

thanks a lot for this small video. this was exactly what I needed.
I find strtok to be very counterintuitive though.

Mr_ToR
Автор

Incredible video, gotta be one of the easiest people to understand. Thanks

Ian-vnfh
Автор

Why is the strtok inside the while loop pointed at NULL?
I can't wrap my head around why it would't be at input.

Does the strtok remember what variable it worked with last?

oliilo
Автор

Thanks man I have the following string could you assist me to split it .
The input as following:-

+CMGR: "REC READ", "12345", , "26/05/21, 21:26:48+18"hello-world OK

The out out should be as following:-

12345
26/05/21
21:26:48
hello-world

eceblog
Автор

What if my string looks like "str, str, str, , str, , , str, str" and I want to parse it so that it would display maybe a '0' or '\0' character where consecutive delimiters occur?

KDR
Автор

In the Linux source code of string.c it says, that strtok() has been replaced by strsep() and there is no definition of strtok() anymore. But I can still use strtok() in my code, what does this mean?

embeddedbastler
Автор

You are a good teacher. Hve you written any books that show alternatives to parsing strings?

danieldixon
Автор

is it possible to use this to split multiple key value pairs, so a nested delimier. e.g. so split key vaue pairs by \n then split each pair to extract the key and value

LeeWalton_
Автор

Good morning sir. Excelent video. I wish to know what is the name and configuration of the ide you used to make this video. I supose it's Atom or Visual Studio Code, but I also wish to know the steps to have this configured as I found it to be very usefull for learning and debug applications. Thank you very much and, again, congatulations for the video.

ssaguiar
Автор

Hello Jacob! This might be a longshot but im going to try either way. I seem to have some trouble understanding when to use the '*' sign in a while loop. Ive just completed a section of my studies which included searching an string for characters. An example is removing '\n' at the end of the string. To solve this i created a pointer (char *arrPTR = string) and placed it in a loop. while(*arrPTR != NULL) -> if(*arrPTR == '\n') then *arrPTR = '\0' if not ->arrPTR++. This was the gist of it. But when i started using strtok i saw that the we dont use the value, i.e *arrPTR, but the adress. while(arrPTR !=NULL), why is that? I thought i understood how this worked, and for me the last example does not make sense, cause arrPTR is just an adress right? So why do we compare an adress to NULL when using strtok and not at other times? Hope this makes some sense, its kinda hard to explain via the comments.

dstange
Автор

if I'm scanning a word from the user, how it will be?

mnarjb
Автор

why no one is showing split string with all occurences of sub-string and return the tokens to double pointer char**, this function is more important than strtok.

alexantollin
Автор

Damn Jacob... them patron bottles. C got you stressed?

aaronbraun
Автор

Can anyone explain to me what the below for loop does and how it works please?
void tokenize(char *line, char **words, int *nwords)
{
*nwords=1;
for(words[0]=strtok(line, " \t\n");
(*nwords<MAX_WORDS)&&(words[*nwords]=strtok(NULL, " \t\n"));
*nwords=*nwords+1
); /* empty body */
return;
}

iamthejustin
Автор

Hi Sir, I do the same with your code, but my input file is 10, 20, 30, 40
When I print out the output, it just show 20, 30, 40 which lost of the first element. Why I got that trouble?

phuongvy
Автор

If I wanted to print the total amount of token how would I do that?

mnyman
Автор

delimiting with space doesn't work. Why?

nikyabodigital