gpt4 book ai didi

c++ - 用另一个词替换字符串中的单个词

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:05 26 4
gpt4 key购买 nike

<分区>

努力寻找一种方法将“他”替换为“他或她”,将“他”替换为“他或她的”,而不像我的代码下面那样将“the”替换为“the or she”:

#include <iostream>
#include <string>

using namespace std;

void myReplace(string& str, const string& oldStr, const string& newStr)
{
if (oldStr.empty())
{
return;
}

for (size_t pos = 0; (pos = str.find(oldStr, pos)) != string::npos;)
{
str.replace(pos, oldStr.length(), newStr);
pos += newStr.length();
}
}

int main()
{
string searchStr;

Beginning:

cout << "Please enter a sentence (Maximum of 100 characters)\n"
<< "Or type 'exit' to close the program\n";
getline(cin, searchStr);

cout << "\nYour input:\n\t" << searchStr;

myReplace(searchStr, "he", "he or she");
cout << "\nReplaced Text\n\t" << searchStr << "\n\n";

goto Beginning;
}

我的程序做了什么...

Input: He is the man
Output: He or she is the or she man

它应该做什么...

Input: He is the man
Output: He or she is the man

任何人都可以帮助我解决这个问题。如果你问……是的,我到处都在谷歌上搜索过。完全不符合我的需要。提前致谢

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com