I want to input an array of integers without giving spaces.
For ex:- 032146548 ,each integer should be stored in array distinctly ,
i.e a[0]=0,a[1]=3,a[2]=2 and so on.
How can i do this ?
I think it's clearer to say "each digit", since it's not at all obvious how many "integers" the character sequence 032146548 represents (the common practice is "one") once you know it's supposed to be several.
The simplest way is to just read it in as a string of digits, then convert each digit to its integer counterpart by subtracting '0':
char line[12];
unsigned int a[10];
if(fgets(line, sizeof line, stdin) != NULL)
{
const size_t digits = strlen(line) - 1;
for(size_t i = 0; i < sizeof a; ++i)
{
if(i < digits && isdigit((unsigned int) line[i]))
a[i] = line[i] - '0';
else
a[i] = 0;
}
}
#include <stdio.h>
int main(){
int a[16];
int i, j, stat;
char ch[2] ={0};
for(i=0;i<16;++i){
if(1!=(stat=scanf("%1d%1[^0-9]", &a[i], ch))){
if(stat==2)
++i;
break;
}
}
for(j=0;j<i;++j)
printf("%d ", a[j]);
printf("\n");
return 0;
}
\n not to stop taking input.
'0'.isdigiton each character to detect when the sequence of digits ends.