strtol()
returns:
On success, the function returns the converted integral number as a long int value. If no valid conversion could be performed, a zero value is returned (0L).
you should re write your code as:
end = buffer;
base = 10;
long int = li;
ele = 0;
while(li=strtol (end,&end, base)){
ele += 1;
}
return ele;
You don't need to explicitly use strstr()
to locate for next integer. strtol()
smart enough and after each conversion it locate end point to next int.
I have written an running code may be your find helpful:
#include <stdio.h> /* printf */
#include <stdlib.h> /* strtol */
int main (){
char buffer[] = "2001 11 223 444 566";
char * end;
long int li;
end =buffer;
int base =10;
int ele = 0;
while(li=strtol (end, &end, base)){
printf("%ld \n", li);
ele += 1;
}
printf("\nNo of elements: %d", ele);
return 0;
}
its output:
2001
11
223
444
566
No of elements: 5