I'm writing a program in C where there's a user input. This input is a string with integer values separated by a space. The numbers (except the first one) have to be stored in an integer array. The first number indicates how many numbers have to be stored (so the size of the array).
What's the easiest way to do this in C? Here's an example:
input--> "5 76 35 95 14 20"
array--> {76, 35, 95, 14, 20}
I've been searching around but I can't find a solution to my question. At the moment, I've tried to store the values of the input in a char array and, when there's a space, I convert this string to an integer using atoi()
and I add it to the integer array. But it prints strange values. This is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char text[10];
scanf("%s", text);
int nums[4];
int indNs = 0;
char lastNum[10];
int indLN = 0;
for(int i = 0; i < 10; i++)
{
if(text[i] == ' ')
{
nums[indNs] = atoi(lastNum);
indNs++;
sprintf(lastNum, "%s", "");
indLN = 0;
} else
{
lastNum[indLN] = text[i];
indLN++;
}
}
nums[indNs] = atoi(lastNum);
for(int i = 0; i < 4; i++)
{
printf("%d\n", nums[i]);
}
}