如何在整个单词中搜索包含三个以上相同字母的单词
How to search words that contain more than three of the same letter through out the entire word
出于某种原因,这段代码正在打印出我列表中的所有单词,而我希望它只打印出三个以上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';
}
}
}
}
有几个问题:
-
string::find_first_of()
不是要使用的正确功能。它在字符串中搜索与其参数中指定的任何字符匹配的第一个字符。换句话说,您的代码确实会查找单个字母z
(因为这是subString
中出现的唯一不同字母)。如果您希望连续查找三个z
,请改用string::find()
。如果您希望在字符串中的任何位置找到三个z
,请使用std::count()
。 -
您没有正确检查返回值。您隐式地将返回值与零进行比较,而您需要与
string::npos
进行比较。 -
wordIndex++
放错了地方。 -
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
}
相关文章:
- C++-字符串是否包含一个带有简单循环的单词
- 遍历 std::set 中包含的所有三重不同值?
- 包含换行符分隔的单词的文件和C++中这些单词的字符串向量的大小是否相同?
- 查找包含指定单词的行(从文件中)
- 如何创建一个包含三种不同类型的向量的向量
- 如果该行包含特定单词 (C++),请打印该行
- 最快/最小方法检查字符串是否包含多个单词
- 如何在Visual Studio 2013 Ultimate Edition中包含某个单词的每一行中插入断点
- 如何从包含多个句子的输入文件中表达单个单词.C
- 如何根据第三个单词在文本文件中排序行
- 分类包含数字和单词的字符串向量
- 如何将消息发送到缓冲区并反向打印前三个单词的消息
- 如何使用并行数组使用 c++ 制作包含数量和单词的列表
- 删除包含单词的行
- C++,我想在字符串中搜索一个字符,并输出包含该字符的所有单词
- 忽略包含特殊单词的字符串(月)
- 如何从包含单词的文本文件制作两个数组
- C++将<单词 A>替换为<单词包含 A>
- 对包含三个项目的数组进行排序
- 如何在整个单词中搜索包含三个以上相同字母的单词