0

What I have is a string with some numbers (unknown amount but has a maximum), for example char string[] = "12 13 14 123 1234 12345"; and I wanna add each number to an array. So let's say I have an array int numbers[50]; and I want to add it to the array so it's like {12, 13, 14, 123, 1234, 12345}.

I've tried a loop through the string that uses a variable as a state machine to detect if it is reading a number but the code ended up looking like spaghetti so I just lost the way I was thinking. I cannot use malloc() or anything that allocates memory for this exercise.

Any ideas?

2
  • Use strtol in a loop using the second parameter to move the pointer to the next space. Commented Apr 9, 2021 at 17:05
  • I would have liked to see the code you wrote Commented Apr 9, 2021 at 17:14

3 Answers 3

1

Here you are.

#include <stdio.h>

int main(void) 
{
    enum { N = 50 };
    int numbers[N];
    
    char *s = "12 13 14 123 1234 12345";
    
    size_t  n = 0;
    char *p = s;
    
    for ( int value, pos = 0; n < N && sscanf( p, "%d %n", &value, &pos  ) == 1; p += pos )
    {
        numbers[n++] = value;
    }
    
    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%d ", numbers[i] );
    }
    
    putchar( '\n' );
    
    return 0;
}

The program output is

12 13 14 123 1234 12345
Sign up to request clarification or add additional context in comments.

5 Comments

Minor: With "%d %n" (add space), allows for after loop assessment with *p == 0 for trailing junk but allowing trialing white-space as in "123 456\n".
@chux-ReinstateMonica I twill update the code.:)
T'was kind of you. ;-)
Thank you for this, could you explain the first for loop?
@DoubtLord The call of sscanf extracts a number from the string pointed to by the pointer p. The variable pos gets the value of extracted characters. This value is used to move the pointer p to point to the next number in the string.
0

The state machine idea is good (albeit overkill; you only have three or four states after all).

You could use both this and Horner's Algorithm to parse the integer.

- set Horner accumulator to 0 and HaveNumber to 0 (false).
- loop:
    - read character
    - if digit:
       - multiply Horner by 10
       - add the value of the digit to Horner
       - set HaveNumber to 1
       - continue
    - if HaveNumber:
         push Horner onto array
         set HaveNumber to 0
    - set Horner to 0
    - if character was a 0 instead of a space:
         break
   

This does not need memory and does not necessarily use any function call (the digit check may be done verifying the ASCII value of the digit between 0x30 and 0x39)

Comments

0

You can use strtok to split string into substrings and then use atoi to convert that into ints.

#include <stdlib.h>
#include <string.h>

int main()
{
    char my_string[] = "12 23 234 2345";
    int my_array[50] = {0};
    
    char* token = strtok(my_string, " ");
    int i = 0;
    while (token)
    {
        my_array[i] = atoi(token);
        token = strtok(NULL, " ");
        i++;
    }
    
    return 0;
}

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.