Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使用数组中的字符串查找所需的特定字母_C_Arrays_String - Fatal编程技术网

C 如何使用数组中的字符串查找所需的特定字母

C 如何使用数组中的字符串查找所需的特定字母,c,arrays,string,C,Arrays,String,如何获取包含p或p的项并返回此数组中出现的匹配数 { SKU: 275, "Royal Gala Apples" }, { SKU: 386, "Honeydew Melon" }, { SKU: 240, "Blueberries" }, { SKU: 916, "Seedless Grapes" }, { SKU:

如何获取包含p或p的项并返回此数组中出现的匹配数

            {  SKU:  275, "Royal Gala Apples"   },
            {  SKU:  386, "Honeydew Melon"      },
            {  SKU:  240, "Blueberries"         },
            {  SKU:  916, "Seedless Grapes"     },
            {  SKU:  385, "Pomegranate"         },
            {  SKU:  495, "Banana"              },
            {  SKU:  316, "Kiwifruit"           },
            {  SKU:  355, "Chicken Alfredo"     },
            {  SKU:  846, "Veal Parmigiana"     },
            {  SKU:  359, "Beefsteak Pie"       },
            {  SKU:  127, "Curry Chicken"       },
            {  SKU:  238, "Tide Detergent"      },
            {  SKU:  324, "Tide Liq. Pods"      },
            {  SKU:  491, "Tide Powder Det."    },
            {  SKU:  538, "Lays Chips S&V"      },
            {  SKU:  649, "Joe Org Chips"       },
            {  SKU:  731, "Allen's Apple Juice" },
            {  SKU:  984, "Coke 12 Pack"        },
            {  SKU:  350, "Nestea 12 Pack"      },
            {  SKU:  835, "7up 12 Pack"         }
    };

这可能是一个简单的解决方案:

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

#define NPROD 6
struct product {
    int sku;
    char *name;
};

struct product products[NPROD] = {
{  275, "Royal Gala Apples"   },
{  386, "Honeydew Melon"      },
{  240, "Blueberries"         },
{  916, "Seedless Grapes"     },
{  385, "Pomegranate"         },
{  127, "Curry Chickens"      }
};

int main()
{
    int i, j, nfound;
    nfound = 0;
    for (i = 0; i < NPROD; i++) {
        for (j = 0; j < (int)strlen(products[i].name); j++) {
            if (products[i].name[j] == 'p' ||
                products[i].name[j] == 'P') {
                /* Do whathever you want with this product */
                printf("%d %s\n", products[i].sku,
                       products[i].name);
                nfound++;
                break;
            }
        }
    }

    printf("found %d matching products\n", nfound);
    return 0;
}
然而,我不确定strbprbk是如何实现的,但是如果它只是针对字符串的大小执行for循环,我也不会感到惊讶。在这种情况下,使用两个strbprbk调用,此解决方案的效率甚至低于第一个解决方案。可能有更好的解决方案,但我希望你能理解。 这是输出:

275 Royal Gala Apples
916 Seedless Grapes
385 Pomegranate
found 3 matching products

请查看strpbrk()的手册页。。。这应该让你开始。。。如果您仍然有问题,请发布代码并询问您的具体问题。
275 Royal Gala Apples
916 Seedless Grapes
385 Pomegranate
found 3 matching products