4

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.

5 Answers 5

5

Use sscanf, or strtol in a loop.

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

2 Comments

Yes ,bu it splits my array into{1,2,3,4,5,9} and continues to loop.How to end the loop ?
Show what you have so it can be fixed.
1

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]);

Comments

0

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
}

Comments

0

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]);

Comments

-1

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.

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.