将字符串拆分为c中的INT数组

时间:2016-10-18 12:46:59

标签: c

我得到的字符串包含以空格分隔的数字。数字可以是单位数,两位数或更多位数。检查示例。

"* SEARCH 2 4 5 12 34 123 207"

我不知道字符串有多长(它包含多少个数字),所以我无法正确启动数组。结果应如下所示:

array = {2,4,5,12,34,123,207}

您对如何执行此操作有任何想法吗?

3 个答案:

答案 0 :(得分:2)

像这样:

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

int main(void){
    char *input = "* SEARCH 2 4 5 12 34 123 207";
    int len = 0;

    sscanf(input, "%*[^0-9]%n", &len);//count not-digits(The Number isn't negative)

    char *p = input + len;
    char *start = p;
    int v, n = 0;
    while(1 == sscanf(p, "%d%n", &v, &len)){
        ++n;//count elements
        p += len;
    }
    int array[n];//or allocate by malloc(and free)
    char *endp = NULL;
    int i;
    for(i = 0; i < n; ++i){
        array[i] = strtol(start, &endp, 10);
        start = endp + 1;
    }
    //check print
    for(i = 0; i < n; ++i)
        printf("%d ", array[i]);
    puts("");
    return 0;
}

答案 1 :(得分:0)

您可以尝试这种方法。它使用临时缓冲区来保存正在处理的当前整数。它还使用动态数组来处理要处理的不同长度的字符串,并在必要时扩展它们。虽然在这种情况下使用strtok会更好。

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

int
main(int argc, char *argv[]) {
    char message[] = "* SEARCH 2 4 5 12 34 123 207";
    char *buffer = NULL;
    int *integers = NULL;

    int buff_size = 1, buff_len = 0;
    int int_size = 1, int_len = 0;
    int ch, messlen, i, first_int = 0;

    /* creating space for dynamic arrays */
    buffer = malloc((buff_size+1) * sizeof(*buffer));
    integers = malloc(int_size * sizeof(*integers));

    /* Checking if mallocs were successful */
    if (buffer == NULL || integers == NULL) {
        fprintf(stderr, "Malloc problem, please check\n");
        exit(EXIT_FAILURE);
    }

    messlen = strlen(message);

    /* going over each character in string */
    for (ch = 0; ch < messlen; ch++) {

        /* checking for first digit that is read */
        if (isdigit(message[ch])) {
            first_int = 1;

            /* found, but is there space available? */
            if (buff_size == buff_len) {
                buff_size++;
                buffer = realloc(buffer, (2*buff_size) * sizeof(*buffer));
            }
            buffer[buff_len++] = message[ch];
            buffer[buff_len] = '\0';
        }

        /* checking for first space after first integer read */
        if (isspace(message[ch]) && first_int == 1) {
            if (int_size == int_len) {
                int_size++;
                integers = realloc(integers, (2*int_size) * sizeof(*integers));
            }
            integers[int_len] = atoi(buffer);
            int_len++;

            /* reset for next integer */
            buff_size = 1;
            buff_len = 0;
            first_int = 0;
        }

        /* for last integer found */
        if (isdigit(message[ch]) && ch == messlen-1) {
            integers[int_len] = atoi(buffer);
            int_len++;
        }
    }

    printf("Your string: %s\n", message);

    printf("\nYour integer array:\n");
    for (i = 0; i < int_len; i++) {
        printf("%d ", integers[i]);
    }

    /* Being careful and always free at the end */
    /* Always a good idea */
    free(integers);
    free(buffer);

    return 0;
}

答案 2 :(得分:-2)

您可以阅读每个字符并验证它是否在> = 48(Ascii为0)且小于= 57(Ascii为9)的范围内。如果是这样的话,将它们读入数组中,否则你可以将它们复制到临时字符串并使用像atoi()

这样的函数转换为int
#include <stdio.h>

int main(int argc, char *argv[])
{
    int j=0,k,res;
    char buff[10];
    while(str[j])
    {   
        if((str[j]>='0')&&(str[j]<='9'))
        {
            k=0;
            while((str[j]!=' ')&&(str[j]!='\0'))
            {
                buff[k]=str[j++];
                k++;
            }
            buff[k]=0;
            res=atoi(buff);
            //Store this result to an array
        }
        j++;
    }
    return 0;
}