Resolved: C – How to split a string with delimiter when sometimes there are no values between delimiter?

In this post, we will see how to resolve C – How to split a string with delimiter when sometimes there are no values between delimiter?

Question:

I am trying to split a string as follows: 1.97E+13,1965.10.30,12:47:01 AM,39.1,23,greece,,,,,10,4.8,4.6,4.6,4.8,4.6,4.7
I am using strtok and giving , as a delimiter but since there are no values between some commas I get a segmentation fault.
What is the correct way to assign null values to consecutive commas?

Best Answer:

Instead of strtok use functions strspn and strcspn.
Here is a demonstration program.
The program output is

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com