Question

I am in trouble about getting integer from string and using it. I have:

char *string = "I 17 24 flying bits";

I want to take 17 and 24 as integer and use them. Pointer casting is out of question, how can i get those integers and use them ?

Was it helpful?

Solution

int a, b;
int count = sscanf(array, "%*s %d %d", &a, &b);
if (count == 3) // then you matched three tokens, proceed, else failed to match

OTHER TIPS

You can use strtok standard library function. Include <string.h> header.

You can use strtok along with atoi() and isdigit(). you can refer this link to get idea. same logic as one of the answer there can be used.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top