carrot_programmer_3
carrot_programmer_3

Reputation: 955

Split a string into an integer array in C

I have a comma delimited string in a C app that I'm putting together and I want to split it into an array of integer values. The string could contain any number of comma delimited values so I also dont have an initial size for the array.

e.g.

"345,3345,35,75,52,386"

and id like to do something like...

int parsedArray[] = parseString("345,3345,35,75,52,386");

This would be a breeze in java or c# but I think I'm a little out of my depth when it comes to C. Any ideas of how to achieve this?

Upvotes: 3

Views: 14291

Answers (4)

Prateek Yadav
Prateek Yadav

Reputation: 942

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

int main ()
{
  char str[] = "7,4200,4450, 500,1600, 500,550,600"; 


// Returns first token 
char *token = strtok(str, ","); 

int size=atoi(token);
int signal[size];
int i=0;

// Keep printing tokens while one of the 
// delimiters present in str[]. 
while (token != NULL) 
{ 
printf("%s\n", token); 
token = strtok(NULL, ","); 
if(token !=NULL){
signal[i]=atoi(token);
printf("%d --", signal[i]);
i=i+1;

}
} 

return 0; 
}

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726639

A function with the signature implied in your post is not possible in C, because a function cannot return a dynamically-sized array. Two choices are possible here:

  • Pass an array and the max count into parseString, or
  • Return a pointer representing a dynamically allocated array, along with its actual size.

The first approach limits the size of the array to some number established by the caller; the second approach comes with a requirement to deallocate the array once you are done with it.

The function that follows the first approach would look like this:

void parseString(int data[], size_t maxSize);

The function that follows the second approach would look like this:

int *parseString(size_t *actualSize);

or like this:

void parseString(int ** data, size_t *actualSize);

The second case can be used to combine the two approaches, although the API would become somewhat less intuitive.

As for the parsing itself, many options exist. One of the "lightweight" options is using strtol, like this:

char *end = str;
while(*end) {
    int n = strtol(str, &end, 10);
    printf("%d\n", n);
    while (*end == ',') {
        end++;
    }
    str = end;
}

Demo.

Upvotes: 2

&#212;rel
&#212;rel

Reputation: 7622

pis a pointer to your string

int parsedArray[1024];                                                     
int nb = 0;

while (nb < sizeof(parsedArray) && *p) {                                   
    parsedArray[nb++] = strtol(p, &p, 10);                                 
    if (*p != ',')                                                         
        break;                                                             
    p++; /* skip, */                                                       
}

nb is the number of parsed int

Upvotes: 1

Brian Agnew
Brian Agnew

Reputation: 272307

I would look at the C function strtok() and use that to iterate through your string and extract each element. Use atoi() to resolve each element to an integer.

Upvotes: 1

Related Questions