Hi,
here i get stuck, i was thinking of using sscanf to turn the input vales
back to individual numbers, but how do i assign the number when i dont
even know how many there are going to be.
Using "sscanf()" to convert an unknown number of tokens is rather
tricky to do. "sscanf()" will return wether a conversion was
successful or not, or whether the end of the string was reached, but
it wont tell you where the next number in the string begins. I'm not
saying it cant be done, just that there are simpler ways to do it.
Solving the problem with "sscanf()" would require using the "%n" field
specification.
Generally I would recommend using "strtok()" for this type of problem.
But since it is a rather simple specific situation a simple
char-by-char processing loop seems more appropriate:
char *cp;
int i = 0;
for ( cp = line ; *cp != '\0' ; cp++ )
if ( isdigit(*cp) && !isdigit(*(cp+1)) )
some_list[i++] = *cp - '0';
The above example is without any error checking, though, and will
process some cases of illegal input. The version below will skip any
amount of non digit separator chars, will accept and convert single
digit numbers, and will quit when encountering a number with more then
one digit.
for ( cp = line ; *cp != '\0' ; cp++ )
{
if ( isdigit(*cp) )
{
if ( isdigit(*(cp+1)) )
{
printf( "bzzzt : number with more then 1 digit found\n" );
break;
}
else
some_list[i++] = *cp - '0';
}
}
Add an "else" part to the outer "if" for checking the non digit
separator chars.
--
Stephan