首页 > 解决方案 > 用另一个词替换字符串中的单个词

问题描述

努力寻找一种方法将“他”替换为“他或她”,将“他的”替换为“他或她的”,而不像我的代码如下所示将“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

任何人都可以帮助我解决这个问题。如果您要问... 是的,我到处搜索谷歌。这该死的东西不符合我的需要。提前感谢

标签: c++codeblocks

解决方案


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

  1. 快速而肮脏的方法:

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

要解决这个问题,您需要考虑ussually单词前后的内容(稍后会详细介绍)。通常它是一个空白区域。这意味着快速解决方法是替换“he”而不是“he”。所以一个句子The something he something确实会给我们The something he or she something

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

假设“He is something he”作为我们的句子,这将变成“He is something he”,让替换起作用。然后修剪最后的字符串将摆脱多余的空格。所以你将拥有:

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

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

  • 句子的第一个/最后一个单词不会以空格开头/结尾。
  • 最后一个单词可能以标点符号结尾,例如.or !,这在前面的示例中不起作用
  • 字符串内的标点符号: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)

列表将是:[He, is, mean, to, the, teacher](注意,我们将失去标点符号,稍后会详细介绍)

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

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. 但我不想要正则表达式......(然后阅读重复的评论

推荐阅读