给定一个字符串,只要您看到单词"ant"替换到字符串中,单词就会"termite"
Given a string whenever you see the word "ant" substitute into the string the word "termite"
我正在尝试在不使用替换函数的情况下将吹动文本上的所有"蚂蚁"单词替换为"白蚁"。我只能替换文本上的第一个单词。有人可以告诉我我在循环中做错了什么吗?谢谢! 附言这也是学校的作业。我必须按照教授的要求以某种方式对其进行编码。
#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"
,因为您有几种情况。
如果你用正则表达式和相关库进行一些挖掘,你可以得到一个更好的方法,它允许ant
或ants
粘在标点符号(如,
、.
或?
(,所以,替换也适用于这些。 请参阅regex(3)
库文档。
相关文章:
- 从字符串变量中逐字符读取单词
- 使用std::mt19937从字符串中返回一个随机单词
- C++-字符串是否包含一个带有简单循环的单词
- 在C++中查找(奇怪的)字符串中的单词
- 当字符串是某个单词时给出输出?
- 替换字符串中的单词,但忽略引号中的单词
- 替换字符串位置 X 中的单词C++
- 视觉C++使用 map 来比较字符串中的每个单词
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- 输出返回编号。等于输入字符串的单词的字符串数
- 在字符串的每个单词的末尾插入字符串
- 分析字符串中的双精度和单词
- 如何使用运算符>>在自定义字符串中输入多个单词?
- 反转字符串中单词的位置,而不更改 O(1) 空格限制中特殊字符的顺序
- 使用正则表达式c++从单词和分隔符之间的字符串中提取所有子字符串
- C++读取大文件并将其保存到字符串中,然后删除特定的随机单词
- 字符串单词的唯一哈希值
- 使用std :: string ::比较(C )的字符串/单词操纵
- 如何比较字符串单词和聊天猜测
- 用C++计算文本文件中的相同字符串/单词