解析 C 中的整数和字符数组

Parse arrays of integers and characters in C

我有两个这样的数组:

char test_results[] = "20 7 1 12 3"
int  test[]         = {3, 8, 9, 12, 6}

我想一个一个地取出test_results的数字,并与test数组的数字进行比较。如果有任何巧合,请打印结果。

我该怎么做?谢谢!

这应该是你需要的

char  text[] = "20 7 1 12 3";
int   test[] = {3, 8, 9, 12, 6};
char *ptr;
int   count;

ptr   = text;
count = sizeof(test) / sizeof(*test);
while (*ptr != '[=10=]')
{
    int value;
    int i;

    value = strtol(ptr, &ptr, 10);
    for (i = 0 ; i < count ; i++)
    {
        if (value == test[i])
            printf("there is a coincidence at the %dth number of the array %d\n", i, value);
    }
}

它会输出

there is a coincidence with the 3th number of the array 12
there is a coincidence with the 0th number of the array 3