Input : [1,3,2,4]
I want to make arr[4] = {1, 3, 2, 4}
from this input using scanf()
. How can I do this in C language?
CodePudding user response:
It is possible to parse input such as you describe with scanf
, but each scanf
call will parse up to a maximum number of fields determined by the given format. Thus, to parse an arbitrary number of fields requires an arbitrary number of scanf
calls.
In comments, you wrote that
I want to find a method to ignore '[', ']', ',' and only accept integer units.
Taking that as the focus of the question, and therefore ignoring the issues of how you allocate space for the integers to be read when you do not know in advance how many there will be, and assuming that you may not use input functions other than scanf
, it seems like you are looking for something along these lines:
int value;
char delim[2] = { 0 };
// Scan and confirm the opening '['
value = 0;
if (scanf("[%n", &value) == EOF) {
// handle end of file or I/O error ...
} else if (value == 0) {
// handle input not starting with a '[' ...
// Note: value == zero because we set it so, and the %n directive went unprocessed
} else {
// if value != 0 then it's because a '[' was scanned and the %n was processed
assert(value == 1);
}
// scan the list items
do {
// One integer plus trailing delimiter, either ',' or ']'
switch(scanf("%d%1[],]", &value, delim)) {
case EOF:
// handle end of file or I/O error (before an integer is read) ...
break;
case 0:
// handle input not starting with an integer ...
// The input may be malformed, but this point will also be reached for an empty list
break;
case 1:
// handle malformed input starting with an integer (which has been scanned) ...
break;
case 2:
// handle valid (to this point) input. The scanned value needs to be stored somewhere ...
break;
default:
// cannot happen
assert(0);
}
// *delim contains the trailing delimiter that was scanned
} while (*delim == ',');
// assuming normal termination of the loop:
assert(*delim == ']');
Points to note:
- it is essential to pay attention to the return value of
scanf
. Failure to do so and to respond appropriately will cause all manner of problems when unexpected input is presented. - the above will accept slightly more general input than you describe, with whitespace (including line terminators) permitted before each integer.
- The directive
%1[],]
attempts to scan a 1-character string whose element is either]
or,
. This is a bit arcane. Also, because the input is scanned as a string, you must be sure to provide space for a string terminator to be written, too. - it would be easier to write a character-by-character parser for your specific format that does not rely on
scanf
. You could also usescanf
to read one character at a time to feed such a parser, but that seems to violate the spirit of the exercise.
CodePudding user response:
int arr[4];
for(int i=0;i<4;i ) scanf("%d",&arr[i]);
Are you asking for this? I was little bit confused with your question, if this doesn't solve your query, then don't hesitate to ask again...
CodePudding user response:
use scanf to read a string input from user then parse that input into an integer array To parse you can use string function "find" to locate the "," and "[]" and then use "atoi" to convert string into integer to fill the destination input array.
CodePudding user response:
You can't do this with scanf, you need to take the whole "[1,2,3,4]" string and parse it. Something like this should work:
int arr[50];
char in[50];
scanf("%s", &in); // Take input
int i, j = 0;
for (i = 0; i < strlen(in); i) {
if (/*check if in[i] is a number*/) {
arr[j ] = in[i] - '0'; // Convert to integer and add to arr
}
}
Note that this only works if the numbers are single digit, shouldn't be too hard to change.
Also it has a fixed size for the array, you could add another for loop to count the amount of numbers in the input.