首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只要你看到"ant“这个词,就会给出一个字符串,然后用”白蚁“这个词替换这个字符串。

只要你看到"ant“这个词,就会给出一个字符串,然后用”白蚁“这个词替换这个字符串。
EN

Stack Overflow用户
提问于 2018-05-20 13:32:58
回答 3查看 103关注 0票数 2

我尝试在不使用replace函数的情况下,将blow文本中的所有"ant“单词替换为"termite”。我只能替换正文中的第一个单词。谁能告诉我在我的循环中我做错了什么?谢谢!附注:这也是学校的作业。我必须按照教授的要求以某种方式对其进行编码。

代码语言:javascript
复制
 #include <string>
 #include <iostream>
 using namespace std;

 void HomeworkHeader();

 string Text = "Auntie saw an ant cross the kitchen counter. Then latter she 
 saw a group of ants cross the floor. But, she was focused on adding a new 2 
 meter antenna to her 40 foot antenna mast. Friends would be coming over to 
 help with the raising and lowering of the antenna mast.";

 string FindAndSubstitutes(string bText, string OldWord, string NewWord);

 int main()
 {
      HomeworkHeader();
      cout << Text << endl;
      cout << endl;
      string Revise = FindAndSubstitutes(Text, "ant", "termite");
      cout << Revise;
      return 0;
  }

  string FindAndSubstitutes(string bText, string OldWord, string 
     NewWord)
  {
   int len = Text.length();
    int OldStrLen = OldWord.length();

    for (int i = 0; i < len; i++)
    {
        int WhereIsAnt = Text.find(OldWord);
        string partBefore = Text.substr(0, WhereIsAnt);
        string partAfter = Text.substr( WhereIsAnt + OldStrLen + 1);
        bText = partBefore + NewWord + partAfter;

     }

     return bText;
     }
EN

回答 3

Stack Overflow用户

发布于 2018-05-20 15:00:26

您需要循环,直到找不到任何出现OldWord的地方,并继续复制子字符串以替换为NewWord,就像您已经做的那样。干杯,希望能有所帮助。

代码语言:javascript
复制
#include <string>
#include <iostream>
using namespace std;

void HomeworkHeader();

string Text = "Auntie saw an ant cross the kitchen counter. Then latter she \
saw a group of ants cross the floor. But, she was focused on adding a new 2 \
meter antenna to her 40 foot antenna mast. Friends would be coming over to \
help with the raising and lowering of the antenna mast.";

string FindAndSubstitutes(string bText, string OldWord, string NewWord);

int main()
{
    HomeworkHeader();
    cout << Text << endl;
    cout << endl;
    string Revise = FindAndSubstitutes(Text, "ant", "termite");
    cout << Revise;
    return 0;
}

string FindAndSubstitutes(string Text, string OldWord, string NewWord)
{
    int len = Text.length();
    int OldStrLen = OldWord.length();
    int WhereIsAnt;

    while( (WhereIsAnt = Text.find(OldWord)) >= 0 )
    {
        string partBefore = Text.substr(0, WhereIsAnt);
        string partAfter = Text.substr( WhereIsAnt + OldStrLen);
        Text = partBefore + NewWord + partAfter;
    }

    return Text;
}
票数 1
EN

Stack Overflow用户

发布于 2018-05-20 14:33:34

您需要考虑一些要点,您使用了Text (全局变量)而不是bText参数,然后您使用字符串的大小进行循环,但是它应该在没有要替换的情况下进行循环。在你的算法中要考虑的最重要的一点是每次你从开始搜索而不是最后找到的位置。

代码语言:javascript
复制
<iframe height="477px" width="100%" src="https://repl.it/repls/RudeLinedGzip?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

代码语言:javascript
复制
#include <string>
#include <iostream>
using namespace std;


string Text = "Auntie saw an ant cross the kitchen counter. Then latter she\ 
saw a group of ants cross the floor. But, she was focused on adding a new 2 \
meter antenna to her 40 foot antenna mast. Friends would be coming over to \
help with the raising and lowering of the antenna mast.";

string FindAndSubstitutes(string bText, string OldWord, string NewWord);

int main()
{
      cout << Text << endl;
      cout << endl;
      string Revise = FindAndSubstitutes(Text, "ant", "termite");
      cout << Revise << endl;
      return 0;
}

string FindAndSubstitutes(string bText, string OldWord, string NewWord)
{
  int len = Text.length();
  int OldStrLen = OldWord.length();
  size_t WhereIsAnt = bText.find(OldWord, 0);
  while(WhereIsAnt != string::npos)
  {
      string partBefore = bText.substr(0, WhereIsAnt);
      string partAfter = bText.substr( WhereIsAnt + OldStrLen );
      bText = partBefore + NewWord + partAfter;
      WhereIsAnt = bText.find(OldWord,WhereIsAnt+1);
  }
  return bText;
}
票数 0
EN

Stack Overflow用户

发布于 2018-05-23 14:58:38

对于您用作示例的文本,您可以通过两次调用来查找和替换函数(对于固定模式),只需搜索" ants "并将其替换为" termites ",然后搜索" ant ",然后将所有出现的内容替换为" termite "。这样就不会导致使用"termiteenna"更改"antenna",因为有几种情况。

如果你深入研究一下正则表达式和相关的库,你可以得到一个更好的方法,它允许antants使用标点符号(如,.?),因此,替换也适用于这些字符。请参阅regex(3)库文档。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50431974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档