如何在整个单词中搜索包含三个以上相同字母的单词

How to search words that contain more than three of the same letter through out the entire word

本文关键字:单词 包含三 单词中 搜索      更新时间:2023-10-16

出于某种原因,这段代码正在打印出我列表中的所有单词,而我希望它只打印出三个以上z的单词

我已经设法解决了代码,在它搜索带有"zz"的单词下方,例如嗡嗡声或暴风雪。我的主要目标是搜索贯穿整个单词的三个z的单词,例如zblizzard或其他东西。

Word* Dictionary::findzs()
{
    int wordIndex = 0;
    cout << "List : " << endl;
    while (wordIndex < MAX_WORDS) {
        string word1 = myWords[wordIndex]->word;
        wordIndex++;
        if (word1.find("zz") != std::string::npos){
            cout << word1 << endl;
        }
    }
    return 0;
}

更新:

bool has_3_zs(const std::string& s)
{
    return std::count(std::begin(s), std::end(s), 'z') >= 3;
}
void Dictionary::has3zs()
{
    int wordIndex = 0;
    string word = myWords[wordIndex]->word;
    while (wordIndex < MAX_WORDS) {
        for (auto& s : { word })
        {
            if (has_3_zs(s))
            {
                std::cout << s << 'n';
            }
        }
    }
    }

有几个问题:

  1. string::find_first_of()不是要使用的正确功能。它在字符串中搜索与其参数中指定的任何字符匹配的第一个字符。换句话说,您的代码确实会查找单个字母z(因为这是subString中出现的唯一不同字母)。如果您希望连续查找三个z,请改用string::find()。如果您希望在字符串中的任何位置找到三个z,请使用 std::count()

  2. 您没有正确检查返回值。您隐式地将返回值与零进行比较,而您需要与string::npos进行比较。

  3. wordIndex++放错了地方。

  4. return myWords[wordIndex]看起来像是越界访问,可能会导致未定义的行为。

我现在明白了。您希望在任意位置匹配至少包含 3 个'z'字符的字符串。

使用std::count .此示例:

#include <algorithm> // std::count
#include <iostream>  // std::cout
#include <iterator>  // std::begin, std::end
#include <string>    // std::string
// This is the function you care about.
// It returns `true` if the string has at least 3 'z's.
bool has_3_zs (const std::string& s)
{
    return std::count(std::begin(s), std::end(s), 'z') >= 3;
}
// This is just a test case. Ignore the way I write my loop.
int main()
{
    for (auto& s : {"Hello", "zWorzldz", "Another", "zStzzring"} )
    {
        if (has_3_zs(s))
        {
            std::cout << s << 'n';
        }
    }
}

指纹:

zWorzldz
zStzzring

编辑

好的,我写了一个更像你的例子。我的循环的编写方式与您的大致相同(在我看来这不是最好的,但我不想增加进一步的混乱)。

// This is the function you care about.
// It returns `true` if the string has at least 3 'z's.
bool has_3_zs (const std::string& s)
{
    return std::count(std::begin(s), std::end(s), 'z') >= 3;
}
struct SomeTypeWithAWord
{
    std::string word; // The bit you care about
    // Allow me to easily make these for my example
    SomeTypeWithAWord(char const * c) : word(c) {}
};
// This will contain the words.
// Don't worry about how I fill it up,
// I've just written it the shortest way I know how.
std::vector<SomeTypeWithAWord> myWords
                                {"Hello", "zWorzldz", "Another", "zStzzring"};
// This is the function you are trying to write.
// It loops over `myWords` and prints any with 3 or more 'z's.
void findzs()
{
    std::cout << "List : n";
    std::vector<SomeTypeWithAWord>::size_type wordIndex = 0;
    while (wordIndex < myWords.size()) // Loop over all the words
    {
        const std::string& testWord = myWords[wordIndex].word;
        if (has_3_zs(testWord)) // Test each individual word
        {
            std::cout << testWord << 'n'; // Print it
        }
        ++wordIndex;
    }
}
int main()
{
    findzs();
}

编辑- BoB TFish是更简单,因此更好的解决方案。

您可以使用 find_first_of 3 次来查看您是否(至少)在整个单词中传播了 3 z。尝试这样的事情:

Word* Dictionary::findzs()
    {
        int wordIndex = 0;
        cout << "List : " << endl;
        size_t where;
        int i;
        string subString = "z"; // or "zZ" if you want uppercase as well
        while (wordIndex < MAX_WORDS) {
            string word1 = myWords[wordIndex]->word;
            where  = 0;
            for (i = 0 ; i < 3 ; i++)
            {
              where =  word1.find_first_of(substring, where);
              if (where == string::npos)
                   break;
              where++; // fix...
            }
            if (i == 3)
            {
                cout << word1 << endl;
            }
            wordIndex++;
        }
        //return myWords[wordIndex]; - this will try to return myWords[MAX_WORDS] which is probably outside array bounds
       return NULL; // or whatever else you see fit
    }