视觉C++查找以 "ing" 结尾且少于或等于 10 个字符的单词
visual C++ find words ending with "ing" and are less than or equal to 10 characters
正如标题所示,我想在我的向量中找到所有符合此规则的单词。
我当前的代码是:
void Dictionary::endingIng() {
string findIng;
string foundIng;
int sevenLetters = 7;
for (Word findIng : deffinitions)
if (findIng.getWord().find("ing") <= sevenLetters) {
foundIng = findIng.getWord();
cout << foundIng << endl;
}
但是,它还返回中间某处包含"ing"的单词和长度超过 10 个字符的单词,例如:
accordingly
barringout
bingo
commandingly
感谢您的帮助。
也许这会有所帮助。添加一个案例以跳过超过 10 个字母的单词。
还要检查单词 <= 10 个字母的"ing"位置。
将rfind
用于边缘情况,如"inginging"
for (Word findIng : deffinitions) {
std::string word = findIng.getWord();
if (word.length() > 10) {
// skip this - word is more than 10 letters including "ing"
continue;
}
size_t pos = word.rfind("ing");
if (pos == word.length() - 3) {
// we got a match "ing" as suffix of this word
// do something with `word`
}
}
您为什么不(使用当前解决方案(检查长度,然后获取最后 3 个值并检查它们是否匹配。
或者你可以去
CString findString = "ing";
int maxLetters = findString.GetLength() + 7; //magic number
foreach(CString s in dictionary)
{
bool b_SmallEnough = (s.Find(findString) <= maxLetters );
bool b_EndsIn = (s.ReverseFind(findString) == (s.GetLength() - findString.GetLength()); //reverse find so it doesn't get the first "ing in bringing and return false because 2 != 5
if(b_SmallEnough && b_EndsIn)
{
std::wcout << s << std::endl;
}
}
或者,布尔b_EndsIn可以是:
bool b_EndsIn = (s.MakeReverse.Find(findString.MakeReverse()) == 0);
这意味着您首先检查结束,并且始终获得最后一场比赛
相关文章:
- 当您在此单词中搜索单词时调整字符数组的大小?
- 反转字符串中单词的位置,而不更改 O(1) 空格限制中特殊字符的顺序
- C ++程序在字符数组中插入单词
- C++ - 使用结构数据类型将单词中的单个小写字符更改为大写,反之亦然
- 延迟逐个字符打印单词
- 创建单词对齐的字符向量
- 在字符数组中存储和恢复一组单词
- 如何用单词排列向量中的每个字符升序?
- 如何在字符指针数组中保留指向每个单词的指针?
- 当找到来自 RHS 的字符"\"时,从主字符串中获取单词或子字符串,然后擦除其余部分
- 视觉C++查找以 "ing" 结尾且少于或等于 10 个字符的单词
- 需要将单词的文本文件存储到C 中的字符阵列中
- 从字符数组中删除单词
- 忽略给定单词之前的所有字符
- 如何计算C++字符数组中的单词
- 如何用新单词替换未知数量的相同字符
- (C++) 打印字符数组字符串的单词时出现问题
- 计数句子中单词的字符数
- 如何计算两个文件中字符单词和行的总数,并将它们相加以得出总数
- 使用getline,c 中的数组以相反的方式将所有字符单词转动