将字符串中的单个单词替换为另一个单词

Replacing single word in a string with another word

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

努力寻找一种方法将单词"he"替换为"他或她","his 替换为"他或她",而不是像我的代码那样将"the"替换为"the"或她":

#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 programn";
getline(cin, searchStr);
cout << "nYour input:nt" << searchStr;
myReplace(searchStr, "he", "he or she");
cout << "nReplaced Textnt" << searchStr << "nn";
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

任何人都可以以任何方式帮助我。 如果你问...是的,我到处搜索过谷歌。不是符合我需求的该死的东西。 提前感谢

有多种方法可以实现您正在尝试做的事情,通过继续您已经拥有的东西,为了使它工作,您将拥有: (快速注意,这将是概念或伪代码,已经有好几年没有使用C++了(

  1. 快速和肮脏的方法:

当您尝试匹配一个单词时,就像您所说的那样,如果该单词包含he,它将被替换,因此:the变得the or she.

要解决此问题,您需要考虑ussually(稍后会详细介绍(在单词之前和之后的内容。通常它是一个空白。这意味着一个快速的解决方案是替换"他"而不是"他"。 所以像The something he something这样的句子确实会给我们The something he or she something.

但就像其他人所说的那样,当句子以您要替换的内容开头时,这会导致问题。这就是为什么你要在最初的句子before and after添加一个空格。

假设"他是他的东西">

作为我们的句子,这将变成"他是他的东西",允许替换工作。然后最后修剪字符串将去除多余的空格。 因此,您将拥有:

searchStr = " " + searchStr + " ";   
myReplace(searchStr, " he ", " he or she ");
trim(searchStr)
  1. 列出单词(向量(,然后替换它们

首先,我们首先假设一个词是由something between two white spaces定义的,由于多种原因,它本质上是错误的:

  • 句子的第一个/最后一个单词不会以空格开头/结尾。
  • 最后一个单词可能以标点符号结束,例如.!,这在上一个示例中不起作用
  • 字符串内的标点符号:he, him and her不起作用
  • he/her这样的特殊标志将再次不起作用。

在这种情况下,我们要做的是通过使用正则表达式(C++ 中的 Regex(来拆分单词,该表达式包含可能划分单词的可能特殊字符。在这里,您可能想要做的事情有很多可能性。

  • 您可能希望通过拆分所有特殊字符来分隔单词(取决于您如何使用它,您最终可能会丢失中文字符等(
  • 您可能希望创建要拆分的内容列表:,: ;_.!?/~'"等。

所以在做这样的事情(伪(之后:

ourString = "He, is mean to the teacher!"
delimiter = "[ ,.!?]".toRegex //whitespace and some punctuation marks
list = split(ourString, delimiter)

列表将是:[他,是,意思,到,老师](注意,我们将丢失标点符号,稍后会详细介绍(

现在我们可以简单地遍历列表,用我们需要的元素替换每个元素并将其连接回来:

string = ""
for(word in list)
string+= if(word.toLowerCase == "he") " he or she " else " " word " "

现在我们将有" He or she is mean to the teacher "(再次,标点符号丢失(

如果我们想保留标点符号怎么办?

如果我们想使用相同的方法,而不是简单地拆分标点符号本身,我们可以使用更复杂的正则表达式(python 中的示例(。完整正则表达式的另一种选择是:

  • 首先遍历字符串并在标点符号前后添加空格
  • 通过仅拆分空格将其拆分为列表
  • 更换流程
  • 将绳子重新组合在一起
string = "He, is !mean."
regex = "[,!.:;]"
string = replace(string, regex with " it ") 
//the string is now: "He ,  is  ! mean . " 
// something to get rid of multiple spaces and make them into a single one
normliseWhiteSpaces(string) 
delimiter = " " 
list = split(string, delimiter) //the list is now [he, ,, is, !, mean, .]
string = ""
for(word in list)
string+= if(word.toLowerCase == "he") " he or she " else " " word " "
//the string is now "He or she , is mean . " so we need to: 
normliseWhiteSpaces(string)
trim(string)
  1. 其他东西完全取决于您的实际目标是什么,您对源数据的期望是什么,等等。
  2. 但我不想要正则表达式...(那么阅读重复的评论(