Pages

Monday, March 8, 2010

Parse the text with integer values

Simply a kind of an interesting task: retrieve all integer values from a string like "10 223 21 67"
#include <stdio.h>
#include <string.h>

int main (int argc, const char * argv[])
{
char strInts[] = "10 223 21 67";
char delim[] = " ";
char *token;
int i;
token = strtok(strInts, delim);
while( token != NULL )
{
sscanf(token, "%d", &i);
printf("%d\n",i);

token = strtok( NULL, delim );
}

return 0;
}

No comments:

Post a Comment