See this main:
int main(void)
{
int i;
int ch;
char str[512];
fgets(str, sizeof str, stdin);
for (i = 0; i <= (strlen(str)); i++)
{
if (str[i] != '\0' && str[i] != '\n')
{
int num = atoi(&str[i]);
printf("%d\n", num);
}
}
return 0;
}
I want to get line with numbers from user and get all the numbers without any spaces or tabs.
For example:
The input 1 2 3.
But in this case this the output:
1
2
2
3
3
So why i received 2 and 3 twice?
forloop that, for no logical reason whatsoever, attempts to process every character in the input and output some number for it. It's immediately obvious that for "1 2 3", five numbers will be printed, since there are five characters in that string that are not nul or newline.