Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C – Attempting to parse numbers from strings and store in array but array seems to have no contents after more than one iteration

My program has the goal of parsing the string "Ge34eks-f10or-Gee59ks". It does this by using strtok to split on delimiter ‘-‘ into three tokens Ge34eks, f10or, and Gee59ks. Each token is passed into num_extraction func with the goal of getting the numbers from each token: 34, 10, 59. I want to keep spaces between each token’s numbers, so they will be stored in an array with a zero in between each number : [34, 0, 10, 0, 59]. When attempting to print array nothing appears. Note: The while loop handles the second and third token, so if it is commented out, the first token number,34,is correctly printed.

Specific issue: Nothing is printed? And no error is given
Guesses: Either something wrong with my count variable or my array is not properly storing the numbers.

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


int* num_extraction(char* a, int* arr, int* index) 
{
    char* p = a;
    //int i = 0;
    while (*p)
    {  // While there are more characters to process...
        if (isdigit(*p) || ((*p == '-' || *p == '+') && isdigit(*(p + 1))))
        {
            // Found a number
            arr[*index] = strtol(p, &p, 10);  // Read number
            //printf("%d\n", val);          // and print it.
            *(index)++;
        }
        else
        {
            // Otherwise, move on to the next character.
            p++;
        }

    }
    return arr;

}

int main()
{
    char str[] = "Ge34eks-f10or-Gee59ks";
    int array[100] = { 0 };
    //int* p = array;
    int count = 0;
    int* q = &count;
 
 
    // Returns first token
    char *token = strtok(str, "-");
    num_extraction(token, array, q);
    count += 2;
   
    // Keep printing tokens while one of the
    // delimiters present in str[].
    
    while (token != NULL) // problem starts here if loop is commented out first token number is printed
    {
        //printf("%s\n", token);
        token = strtok(NULL, "-");
        num_extraction(token, array, q);
        count += 2;
    }
    
    for(int i = 0; i < count; i++)
    {
        printf("%d\n", array[i]);
    }
    //printf("%d\n", count);
    return 0;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

look here – see inline comments

while (token != NULL) 
{
    // token not null here from last loop
    token = strtok(NULL, "-");
    // now token is null if last
    num_extraction(token, array, q);
    count += 2;
}

you need to do

// get next token then test if NULL
while ((token = strtok(NULL, "-")) != NULL) 
{
    num_extraction(token, array, q);
    count += 2;
}

or

// get next token then test if NULL
for(;;)
{
   token = strtok(NULL, "-");
    if(token == NULL) break;
    num_extraction(token, array, q);
    count += 2;
}

or even

 while (token = strtok(NULL, "-")) 
    {
        //printf("%s\n", token);
        num_extraction(token, array, q);
        count += 2;
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading