Pregunta

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 ?

¿Fue útil?

Solución

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

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top