【问题标题】:Replacing single word in a string with another word [duplicate]用另一个单词替换字符串中的单个单词[重复]
【发布时间】:2018-06-19 20:06:20
【问题描述】:

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

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

【问题讨论】:

  • 你不能像你一样使用简单的查找替换,你必须检查上下文以确保匹配整个单词。一个词和另一个词的区别是什么?哦,别忘了标点符号不应该算在“单词”中。
  • 你想替换下面的“他”(带空格)-->“他或她”“他”(两个空格)-->“他或她”
  • 你的程序永远不会因为那个讨厌的goto 语句而退出,并且它不能编译,缺少一些包含。
  • 哦,从不使用goto而不是循环。
  • @Someprogrammerdude 我缺乏英语肯定会害死我。正如您所指出的,对字符串进行标记并比较整个单词确实更好。

标签: c++ codeblocks


【解决方案1】:

有多种方法可以实现你想要做的事情,通过继续你已经拥有的东西,为了让它发挥作用,你将拥有:(快速说明,这将是概念或伪代码,尚未使用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 定义的,由于多种原因这本质上是错误的:

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

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

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

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

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 "(同样,标点符号丢失了)

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

如果我们想使用相同的方法,我们可以使用更复杂的正则表达式(an example in 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. 但我不想要正则表达式...(那么Read the duplicate comment

【讨论】:

  • 如果he开始句子呢?
  • 我说:“在字符串的两侧添加一个空格”。这将解决开始或结束“他”的问题,之后,修剪将摆脱它们。我在打电话,在公共汽车上,回家后我会解释,并添加一些替代方案。
  • 感谢您的回答。我现在将尝试修剪方法并报告回来
  • 修剪什么时候进行?在查找和替换之前,还是之后?
  • 它确实有效,但是以“he”开头的句子的问题在那里
猜你喜欢
  • 1970-01-01
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-05
  • 2023-04-03
  • 2015-03-02
相关资源
最近更新 更多