1
I made the following code that takes a string of 3 numbers separated by space, then delimits them and allocates each number in an array position, at the end it prints the array, the code works but only without the 7° and 8° lines. I’d like someone to tell me what the mistake is and teach me how to make it work even though they’ve received other values before.
#include <stdio.h>
#include <string.h>
int main()
{
int num;
scanf("%d", &num);
int i = 0;
char str[4], array[3];
scanf ("%[^\n]", str);
char * pch;
fflush(stdin);
pch = strtok(str, " ");
while(pch != NULL)
{
array[i] = *pch;
pch = strtok(NULL, " ");
i++;
}
for (int i = 0; i < 3; ++i)
{
printf("%c ", array[i]);
}
return 0;
}