Brdjo
Brdjo

Reputation: 43

Convert char array to int array?

I'm beginner in C. I have an char array in this format for example "12 23 45 9". How to convert it in int array {12,23,45,9}? Thanks in advance.

Upvotes: 4

Views: 6415

Answers (5)

Caleb
Caleb

Reputation: 124997

The traditional but deprecated way to do this would be to use strtok(). The modern replacement is strsep(). Here's an example straight off the man page for strsep():

char **ap, *argv[10], *inputstring;

for (ap = argv; (*ap = strsep(&inputstring, " \t")) != NULL;)
    if (**ap != '\0')
        if (++ap >= &argv[10])
            break;

That breaks inputstring up into pieces using the provided delimiters (space, tab) and iterates over the pieces. You should be able to modify the above to convert each piece into an int using atoi(). The main problem with strsep() is that it modifies the input string and is therefore not thread safe.

If you know that the input string will always contain the same number of ints, another approach would be to use sscanf() to read all the ints in one go:

char *input = "12 23 45 9";
int output[5];

sscanf(inputstring, "%d %d %d %d %d", &output[0], &output[1], &output[2], &output[3], &output[4]);

Upvotes: 1

Christian Rau
Christian Rau

Reputation: 45948

What about:

const char *string = "12 23 45 9";
int i, numbers[MAX_NUMBERS];         //or allocated dynamically
char *end, *str = string;
for(i=0; *str && i<MAX_NUMBERS; ++i)
{
    numbers[i] = strtol(str, &end, 10);
    str = end;
};

Though it maybe that you get a trailing 0 in your numbers array if the string has whitespace after the last number.

Upvotes: -1

Muthu Ganapathy Nathan
Muthu Ganapathy Nathan

Reputation: 3307

You will need stdlib.h

//get n,maxDigits
char** p = malloc(sizeof(char*) * n);
int i; 
for(i=0;i<n;i++)
p[i] = malloc(sizeof(char) * maxDigits);

//copy your {12,23,45,9}  into the string array p, or do your own manipulation to compute string array p.

int* a = malloc(sizeof(int) * n);
int i;         
for(i=0;i<n;i++)
a[i] = atoi(p[i]);

Upvotes: 0

SE Does Not Like Dissent
SE Does Not Like Dissent

Reputation: 1825

You can calculate the individual digits by using the following technique (but it won't convert them into the whole number):

Note I am using an int iteration loop to make it readable. Normally you'd just increment the char pointer itself:

void PrintInts(const char Arr[])
{
    int Iter = 0;
    while(Arr[Iter])
    {
        if( (Arr[Iter] >= '0') && (Arr[Iter]) <= '9')
        {
            printf("Arr[%d] is: %d",Iter, (Arr[Iter]-'0') );
        }
    }
    return;
}

The above will convert the ASCII number back into an int number by deducting the lowest ASCII representation of the 0-9 set. So if, for example, '0' was represented by 40 (it's not), and '1' was represented by 41 (it's not), 41-40 = 1.

To get the results you want, you want to use strtok and atoi:

//Assumes Numbers has enough space allocated for this
int PrintInts(const int Numbers[] const char Arr[])
{
    char *C_Ptr = strtok(Arr," ");
    int Iter = 0;
    while(C_Ptr != NULL)
    {
        Numbers[Iter] = atoi(C_Ptr);
        Iter++;
        C_Ptr = strtok(NULL," ");
    }
    return (Iter-1); //Returns how many numbers were input
}

Upvotes: 0

Fred Foo
Fred Foo

Reputation: 363537

Use sscanf, or strtol in a loop.

Upvotes: 5

Related Questions