I want to implement simple task:
I have single line input with numbers
:
2 1 2 3
And i want to print it so currently this works fine for most of scenarios:
char str[512];
for (i = 0; i < (strlen(str)); i++)
{
if (str[i] != '\0' && !isspace(str[i]))
{
int num = atoi(&str[i]);
printf("%d ", num);
}
}
Now in case my input is with number
with mote then one digit
things start to mess.
For example:
2 12
In this case i can see that i have this input:
2 12 2
So in case i have number > 9
i can just raise i
by 1
:'
if (i > 9)
i++;
And in case my number
is with 3 digits
i can just do i+=2
but is there is another solution maybe that is more 'clean' ?
EDIT
fgets(str, sizeof str, stdin);
char *ptr;
long ret;
for (i = 0; i < (strlen(str)); i++)
{
if (str[i] != '\0' && !isspace(str[i]))
{
ret = strtol(str[i], &ptr, 10);
printf("%ld\n", ret);
}
}