C++找到以"ing"结尾且小于或等于10个字符的单词

Mac*_*ius 0 c++ visual-c++

正如标题所示,我想找到符合此规则的载体中的所有单词.

我目前的代码是:

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;
    }
Run Code Online (Sandbox Code Playgroud)

但是,它还会返回在中间某处包含"ing"的单词以及长度超过10个字符的单词,其中一些示例为:

accordingly
barringout
bingo
commandingly
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助.

Jos*_* D. 5

也许这可以提供帮助.添加案例以跳过超过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`
   }
}
Run Code Online (Sandbox Code Playgroud)