How to print Nth word of a C char array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 Answers
24th Aug 2021, 4:39 PM
Rohit
+ 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