0

I am currently trying to figure out how to process an input of such format: [int_1,...,int_N] where N is any number from interval <1, MAX_N> (for example #define MAX_N 1000). What I have right now is fgets to get it as string which I then, using some loops and sscanf, save into an int array.

My solution is, IMO, not the most elegant and functional, but that's because of how I implement it. So what I'm asking I guess is how you guys would solve this problem, because I've ran out of ideas.

Edit: adding the code for string -> int array

int digit_arr[MAX_N];
char input[MAX_N];

//MAX_N is a constant set at 1000
//Brackets and spaces have been removed at this point

for (i = 0; i < strlen(input); i++) {
  if(sscanf(&input[i+index_count],"%d,", &digit_arr[i]) == 1){
    while (current_char != ',') {
      current_char = input[i+index_count+j];
      index_count++;
      j++;
      if ((index_count+j+i) == strlen(input)-1){
        break;
      }
   }
}
Nillaasek
  • 3
  • 1
  • 4
  • 3
    If the code works this might be more of a Code Review site post, including the code of course. But even here, without the code you’ve written we can’t give any pointers because there’s many ways to do this and we don’t know if you’re already doing it well – Sami Kuhmonen Dec 02 '18 at 08:09
  • If you want avoid the impression that you just want us to do your homework for you, you should include your best effort in the question. We can critique it. If it is working code, it could be posted on [Code Review](https://codereview.stackexchange.com/) instead. You could also look at previous similar questions — [How to copy all the numbers from a string](https://stackoverflow.com/questions/53572030/) springs to mind (there's a couple of differences — that isn't limited to 1000 entries, and the brackets used are round brackets (parentheses) rather than square brackets). – Jonathan Leffler Dec 02 '18 at 08:45
  • To your point, there is nothing "inelegant" about your proposed solution -- that's probably the way I would go about it. If you are stuck, then provide [**A Minimal, Complete, and Verifiable Example (MCVE)**](http://stackoverflow.com/help/mcve) showing where and what the issue is and we can all help further. – David C. Rankin Dec 02 '18 at 08:49
  • `My solution is` post your solution. `an input of such format: [int_1,...,int_N]` I find this underspecified. Is input in ASCII characters or binary? `fgets to get it as string` is input terminated by a newline or EOF? why don't you use just `scanf`? – KamilCuk Dec 02 '18 at 08:51
  • @KamilCuk ASCII, newline, and because, as far as I know, you need a set format for `scanf` – Nillaasek Dec 02 '18 at 09:04
  • To everyone else- you're right, I've added my code – Nillaasek Dec 02 '18 at 09:05
  • "set format"? You can just `"%d"` and then use `getchar()` to get the next `,` or `]` depending if it's end. How many characters do you get? How is `digit_arr` defined and declared? How is `input` defined and declared? Why don't you check the `[` and `]` characters and not handle input error? Is this not relevant? Is there a constant count of numbers? – KamilCuk Dec 02 '18 at 09:08
  • @KamilCuk: There'll be a varying number of numbers — the question title indicates as much, and the `int_N` notation implies it. Indeed, the specification "where N can be any number from interval `<1, MAX_N>`" does state that. There's also a question of where blanks are permitted — the 'getchar()` technique might not work well if blanks are permitted (or are simply present despite not being permitted) in the input. – Jonathan Leffler Dec 02 '18 at 09:10

1 Answers1

0

My personal variant:

char const* data = input; // if input is NOT a pointer or you yet need it unchanged
for(;;)
{
    int offset = 0;
    if(sscanf(data, "%d,%n", digit_arr + i, &offset) == 1)
    {
        ++i;
        if(offset != 0)
        {
            data += offset;
            continue;
        }
    }
    break;
}

You might finally ckeck if all characters in the text are consumed:

if(*data)
{
    // not all characters consumed, input most likely invalid
}
else
{
    // we reached terminating null character -> fine
}

Note that my code as is does not cover trailing whitespace, you could do so by changing the format string to "%d, %n (note the added space character).

Aconcagua
  • 24,880
  • 4
  • 34
  • 59