1

I've been trying to implement a function that converts something like the following:

12 13 3 4 0

into an integer array, clearing the whitespaces: int array[size] = {12,13,3,4,0};

Code (there's 2 implementations I've tried):

void string_to_int(char *line, int array[], int size) {

    int i = 0;

    // First implementation:

    char *ptr = line;

    while (*ptr != '\n')
    { 
        sscanf(ptr, "%d%n", &array[i], &offset);
        ptr += offset;
        i++;
    }


    // Second implementation:
    for(i = 0; i < size; i++)
    {
        array[i] = line[2*i] - '0';
    }
}

The first implementation kinda worked in a way, but I get valgrind errors in the sscanf line. The second, doesn't work with numbers >10

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 3
    Look into [`strtok`](http://www.cplusplus.com/reference/cstring/strtok/). The second method does not work for obvious reasons, confirmyou understood this. – Jabberwocky Nov 23 '17 at 13:29
  • Have a look at [this answer](https://stackoverflow.com/a/47451778/28169), it would be easy to adapt just by adding code to write into the array. – unwind Nov 23 '17 at 13:31
  • Also please [edit] your question and show us how you would call `string_to_int`. – Jabberwocky Nov 23 '17 at 13:32
  • @MichaelWalz god man, THANK YOU!! didnt know that strtok – José Correia Nov 23 '17 at 13:46

2 Answers2

1

You can use strtok to get one number as string token from the whole string, then convert it to integer. you have to include string.h to use the function.

The C library function char *strtok(char *str, const char *delim) breaks string str into a series of tokens using the delimiter delim.

Here, Your delimiter will be a white space " ".

The first call to strtok() returns a pointer to the first token in the string pointed to by s1. Subsequent calls to strtok() must pass a NULL pointer as the first argument, in order to get the next token in the string. The set of delimiters used in each of these calls to strtok() can be different from one call to the next.

here is the code:

 char str[20] = "1 2 3 4 5 6 10 12";
 char *token;

 int array[20];
 int top=-1;
 int i;

 token = strtok(str, " ");
 while( token != NULL )
 {
      top++;
      array[top] = atoi(token);

      token = strtok(NULL, " ");

 }

for(i=0;i<=top;i++)
{
    printf("%d ",array[i]);
}

Note: This code works perfectly for your logic :)

Reference:

Jay Joshi
  • 868
  • 8
  • 24
  • Exactly!! Thats the exact implementation i just came up with after they mentioned strtok above!! Thank you very much guys, been on this for a day I searched a lot for an answear to this, and didnt find anything, so anybody reading this, upvote this beauty – José Correia Nov 23 '17 at 13:50
  • I am very much happy that I tried to help :D. Keep using SO. It's ever wonderful here :) – Jay Joshi Nov 23 '17 at 13:53
0

Maybe easiest is a loop that goes over the line, e.g.

char *ptr= line;
int i= 0;
while (*ptr) {
    if (*ptr>='0'&&*ptr<='9') {
        arr[i]= 0;
        while (*ptr>='0'&&*ptr<='9') arr[i] = arr[i]*10 + *ptr++ - '0';
        i++;
        if (i>=N) break;  // array full
    }
    else ptr++;
}
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41