Reputation: 59
Input is like the image above. We have to take the input and add it in an array until there is no comma on the end.
Thanks in advance.
Upvotes: -1
Views: 50
Reputation: 161
Assuming you type the given line 2,3,4,5,6
and press enter to take input, you can easily take input with comma as ending factor with the help of fgets()
and strtok()
functions.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_SIZE 100
int main(){
char buffer[512];
fgets(buffer, 512, stdin);
int arr[MAX_SIZE], index=0;
char* temp = strtok(buffer, ",\n");
while(temp!=NULL){
int x = atoi(temp);
arr[index]=x;
index++;
temp = strtok(NULL,",\n");
}
printf("The output is:\n");
for(int i=0; i<index; i++) printf("%d ",arr[i]);
printf("\n");
return 0;
}
Upvotes: 1