Reputation: 57
I´m having some troubles with my C project.
I read a line using fgets(line, 1024, stdin)
. In the line, there should be exactly 4 arguments separated by white spaces, if not, the program should write a warning. Something like this:
"1f 2 4 34" --> "ok"
"af b v" --> "warning"
"a bbgd c v d" --> "warning"
I was thinking of using a "strtod" function loop, however I´m not sure how. This is my idea:
char * ptr;
int i = 0;
ptr= strtok (line," ");
while (ptr!= NULL) /*I would like to count the white spaces*/
{
i++;
ptr= strtok (NULL, " "); /*I suppose this part is not correct*/
}
if(i != 3) /*3 white spaces --> 4 arguments*/
{...}
Thank you for any answer.
Upvotes: 0
Views: 256
Reputation: 741
$ man strtok
is your friend. The routine eats all occurrences of the delimiter group and returns the address of a null-terminated string.
Don't try to count the spaces, count the number of times strtok(3) returns a non-null value.
Upvotes: 1