How to split string by space into words with C? | SoloLearn: Learn to code for FREE!

+2

How to split string by space into words with C?

Hi. It is really easy to split strings with python by using it's built in function. But I cannot do it with C. I want to split a string by space and make the words separate. Please help me to do this.

7/4/2020 2:19:11 AM

Mahdee Mohammad

2 Answers

New Answer

+3

Consider using either sscanf() or strtok() to break up a string by space delimiters.

+2

Logic is not difficult. Scan the string from left to right, keep collecting the chars till you encounter a space, when you do encounter a space, you know it is a word boundary. so print the word and proceed from last index +1 (where last index is an index of the space) Repeat till the whole string is processed. If you don't want to do char by char then look for how to use strtok function.