Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C program to convert input string of space separated ints into an int array

Tags:

arrays

c

io

Question:

I want to make a C program that takes a string of space separated ints as input (positive and negative, variable number of digits) and converts the string to an int array.

There is another question on reading ints from a string input into an array on Stack Overflow but it doesn't work for numbers of digit length more than 1 or negative numbers.

Attempt:

#include <stdio.h>
int main () {
  int arr[1000], length = 0, c;
  while ((c = getchar()) != '\n') {
    if (c != ' ') {
      arr[length++] = c - '0';
    }
  }
  printf("[");
  for ( int i = 0; i < length-1; i++ ) {
    printf("%d,", arr[i]);
  }
  printf("%d]\n", arr[length-1]);
}

If I enter the following into terminal:

$ echo "21 7" | ./run
$ [2,1,7]

This is the array I get: [2,1,7] instead of [21,7]

If I enter the following:

$ echo "-21 7" | ./run
$ [-3,2,1,7]

I get: [-3,2,1,7] instead of [-21,7] which makes no sense.

However, if I enter:

$ echo "1 2 3 4 5 6 7" | ./run
$ [1,2,3,4,5,6,7]

Note: I am assuming that the input it always a string of space separated integers.

like image 939
user6005857 Avatar asked May 30 '26 21:05

user6005857


2 Answers

Complete program (adapted from this answer by @onemasse) (no longer needs invalid input to stop reading input):

#include <stdio.h>
#include <stdlib.h>

int main () {
    int arr[1000], length = 0, c, bytesread;
    char input[1000];
    fgets(input, sizeof(input), stdin);
    char* input1 = input;
    while (sscanf(input1, "%d%n", &c, &bytesread) > 0) {
        arr[length++] = c;
        input1 += bytesread;
    }
    printf("[");
    for ( int i = 0; i < length-1; i++ ) {
        printf("%d,", arr[i]);
    }
    printf("%d]\n", arr[length-1]);
    return 0;
}

From the scanf/sscanf man page:

These functions return the number of input items assigned. This can be fewer than provided for, or even zero, in the event of a matching failure.

Therefore, if the return value is 0, you know that it wasn't able to convert anymore.

Sample I/O:

$ ./parse
1 2 3 10 11 12 -2 -3 -12 -124
[1,2,3,10,11,12,-2,-3,-12,-124]

NOTE: I am currently unsure of exactly how this works. I will look into it. However, if anyone understands, please edit this post or leave a comment.

like image 99
Arc676 Avatar answered Jun 02 '26 14:06

Arc676


This is a barebones version (no error checking, a trailing space should be left after the numbers), I am sure you can pick up from here:

int main(void)
{
    int c;
    int i, num = 0, neg = 0;
    while ((c = getchar()) != EOF) {
        if (c != ' ') {
            if (c == '-') {
                neg = 1;
            } else {
                i = c - '0';
                num = num * 10 + i;
            }
        } else {
            (neg == 1) ? num *= -1 : num;
            printf("%d\n", num + 2);    // this is just to show that you indeed get an integer and addition works
            num = 0;
            neg = 0;
        }
    }
}
like image 31
babon Avatar answered Jun 02 '26 16:06

babon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!