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

时间:2018-06-19 20:06:20

标签: c++ codeblocks

努力寻找一种方法,用“他或她”代替“他”,用“他或她”代替“他”,而不用下面的代码代替“ 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 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

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

1 个答案:

答案 0 :(得分:0)

通过继续使用已有的东西,有多种方法可以实现您想做的事情,以使其起作用,您将拥有:(快速说明,它是概念或伪代码,没有使用过C ++在相当长的时间内)

  1. 快速而肮脏的方法:

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

要解决此问题,您需要考虑一个单词前后ussually(后面会详细介绍)的含义。通常它是一个空白。这意味着一个快速的解决方法是替换“ he”而不是“ he”。 因此,像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这样的特殊符号将再次不起作用。

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

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

因此,在执行了以下操作(伪)之后:

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

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

如果我们要使用相同的方法,则可以使用更复杂的正则表达式(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
相关问题