How to parse a string separated by commas?
c
Solution
Modified from cplusplus `strtok` example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char str[] ="1,2,3,4,5";
char *pt;
pt = strtok (str,",");
while (pt != NULL) {
int a = atoi(pt);
printf("%d\n", a);
pt = strtok (NULL, ",");
}
return 0;
}
Problem
``` Char *strings = "1,5,95,255" ``` I want to store each number into an int variable and then print it out. For example the output becomes like this. Value1 = 1 value2 = 5 Value3= 95 value4 = 255 And I want to do this inside a loop, so If I have more than 4 values in strings, I should be able to get the rest of the values. I would like to see an example for this one. I know this is very basic to many of you, but I find it a little challenging. Thank you