How to write one line in five variables in C?
The goal is this: the user enters five numbers separated by spaces. they can be 1 digit 2 digit and so on. Question: how to write these five numbers not in one char variable, but in five variables (five arrays). Example: user entered
1 423 3 53 6
Variable a = 1 , b = 423 , c = 3 , d = 53 , d = 6 , with each element of the array containing one number: b [0] = 4 , b [1] = 2
I am writing in C, a program without an interface (it looks like a command line).
Here is the code:
int i, j, a1,a2,a3,a4; char inp[50], a1[5],a2[5],a3[5],a4[5],a5[5]; scanf("%s", &inp); for(i=0; inp[i]!=" ";i++){ a1[i]=inp[i]; } for(i=i; inp[i]!=" ";i++){ j=0; a2[j]=inp[i]; j++; } for(i=i; inp[i]!=" ";i++){ j=0; a3[i]=inp[i]; j++; } for(i=i; inp[i]!=" ";i++){ j=0; a4[i]=inp[i]; j++; } for(i=i; inp[i]!=" ";i++){ j=0; a5[i]=inp[i]; j++; }