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

4
  • 3
    Look into strtok. The second method does not work for obvious reasons, confirmyou understood this. Commented Nov 23, 2017 at 13:29
  • Have a look at this answer, it would be easy to adapt just by adding code to write into the array. Commented Nov 23, 2017 at 13:31
  • Also please edit your question and show us how you would call string_to_int. Commented Nov 23, 2017 at 13:32
  • @MichaelWalz god man, THANK YOU!! didnt know that strtok Commented Nov 23, 2017 at 13:46

2 Answers 2

2

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:

Sign up to request clarification or add additional context in comments.

2 Comments

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
I am very much happy that I tried to help :D. Keep using SO. It's ever wonderful here :)
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++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.