0

我有 6 个 const 字符串(每个 5 个字母)

我得到了几个词(在这 6 个词中)。

我想计算每个单词出现了多少次。

如何在 C 中实现它?

我努力了:

  char searchEngineNames[6][5] = { "waze_", "faceb", "fours", "googl",
        "fueli", "yello" };

    static void foo(const char* res_name, int success, void *context, char *last_modified) {
        if (success){
                for (int i=0; i<6; i++)
                {
                    char substringFiveChars[6];

                    strncpy(substringFiveChars, res_name, 5);

                            char substringFiveChars[6]; 

                            substringFiveChars[5] = 0;

                    if (strcmp(searchEngineNames[i],substringFiveChars) == 0)
                    {
                    ... 
                    }
    ..
                }

例如对于这个流:

"wooo_","wooo_","faceb","wooo_","google"

我最终会得到:

"wooo_" 3 times

"faceb" 1 times 

"google" 1 times 

"fours" 0 times

"fuelil" 0 times

"yello" 0 times
4

1 回答 1

0

我使用 2 个数组而不是 1 个。

char searchEngineNames[6][5] = { "wooo_", "faceb", "fours", "google",
        "fuelil", "yello" };

int searchEngineCounts[6] = {0,0,0,0,0,0};



static void foo(const char* res_name,
        int success, void *context, char *last_modified) {
    if (success) {

        int i = 0;
        for (i; i < 6; i++) {
            char substringFiveChars[6];

            strncpy(substringFiveChars, res_name+7, 5);

            substringFiveChars[5] = 0;

            if (strcmp(searchEngineNames[i], substringFiveChars) == 0) {
                searchEngineCounts[i]++;

                if (searchEngineCounts[i] < 3) {

...
                }
            }
        }

    }
}
于 2013-07-08T09:28:13.143 回答