简体   繁体   中英

convert array of characters to array of integers in C

I'm passing in an argument to a C program:

program_name 1234

int main (int argc, char *argv[]) {

    int     length_of_input = 0;    
    char*   input           = argv[1];


    while(input[length_of_input]) {
        //convert input from array of char to int
        length_of_input++;
    }
}

I want to be able to use each digit of the argument passed into the function separately as an integer. atoi(input[]) throws a compile-time error.

This code doesn't compile:

while(input[length_of_input]) {
    int temp = atoi(input[length_of_input]);
    printf("char %i: %i\n", length_of_input, temp);
    length_of_input++;
}
int i;
for (i = 0; input[i] != 0; i++){
    output[i] = input[i] - '0';
}

Seeing as this is homework you could also do

output[i] = input[i] - '0';

but be careful that input[i] is actually a digit (ie it's between '0' and '9' )!

First you have to check how much space you need to allocate for the integer array. This can be done with strlen() function or iterating trough the string and checking how many valid characters are found. Then you must iterate through the string and convert every (valid) character to equivalent integer number. It is hard to use atoi() or scanf() family of functions here since they except array as input. Better solution would be to write your own little converter function or snippet for the conversion.

Here is small example app which converts string to array of ints. If the character is not a valid decimal digit, -1 is placed into array.

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

int main(int argc, char *argv[])
{
    int length, i;
    int *array;
    char *input = argv[1];

    /* check if there is input */
    if(input == NULL) return EXIT_FAILURE;

    /* check the length of the input */
    length = strlen(input);
    if(length < 1) return EXIT_FAILURE;

    /* allocate space for the int array */
    array = malloc(length * sizeof *array);
    if(array == NULL) return EXIT_FAILURE;

    /* convert string to integer array */
    for(i = 0; i < length; ++i) {
        if(input[i] >= '0' && input[i] <= '9')
            array[i] = input[i] - '0';
        else
            array[i] = -1; /* not a number */
    }

    /* print results */
    for(i = 0; i < length; ++i)
        printf("%d\n", array[i]);

    /* free the allocated memory */
    free(array);

    return EXIT_SUCCESS;
}

Also check these questions:

You can to test if argument is a number whith isdigit()

http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

and use atoi function .

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

And be careful in use

char*   input           = argv[1];

copy the string from argv to input (after to use malloc), it's better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM