给定一个字符串,只要您看到单词"ant"替换到字符串中,单词就会"termite"

Given a string whenever you see the word "ant" substitute into the string the word "termite"

本文关键字:字符串 单词 ant 替换 termite 一个      更新时间:2023-10-16

我正在尝试在不使用替换函数的情况下将吹动文本上的所有"蚂蚁"单词替换为"白蚁"。我只能替换文本上的第一个单词。有人可以告诉我我在循环中做错了什么吗?谢谢! 附言这也是学校的作业。我必须按照教授的要求以某种方式对其进行编码。

#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;
}

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

#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;
}

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

<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>

#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;
}

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

如果你用正则表达式和相关库进行一些挖掘,你可以得到一个更好的方法,它允许antants粘在标点符号(如,.?(,所以,替换也适用于这些。 请参阅regex(3)库文档。