문제

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 ?

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top