+ 2

How to print Nth word of a C char array?

How do i print the Nth word in a character array. For example. char mynames[15] = "David Holmes"; How do i print the "David" only or the "Holmes" only. If it was Python, i would: mynames = "David Holmes" tolist = mynames.split() print (tolist[0]) But since in C a string is already an array right, so i figured there is need to convert it to a array again 😕

24th Aug 2021, 3:32 PM
David Holmes Ng'andu
David Holmes Ng'andu - avatar
2 Antworten
24th Aug 2021, 4:39 PM
minirkk
minirkk - avatar
+ 1
you can use strtok() to split in c. it takes a string and a delimiter. delimiter in this case is " " space. https://code.sololearn.com/cSm9Y3tbQEOa/?ref=app in the example I printed each word on a new line, you can instead append each word to an array and print them like in your python example.
24th Aug 2021, 4:47 PM
Bahhaⵣ
Bahhaⵣ - avatar