替换句子中的单词?

Replacing words in a sentence?

我正在尝试用 "pirate pair" 替换多个单词,例如:

正常:"Hello sir, where is the hotel?"

海盗:"Ahoy matey, whar be th' fleagbag inn?"

这是我之前尝试过的:

#include<iostream>
#include<string>
#include<conio.h>

using namespace std;

void speakPirate(string s);

int main()
{
    string phrase;

    cout << "Enter the phrase to Pirate-Ize: ";
    getline(cin, phrase);

    speakPirate(phrase);

    _getch();
    return 0;
}

void speakPirate(string s)
{
    int found;

    // list of pirate words
    string pirate[12] = { "ahoy", "matey", "proud beauty", "foul blaggart", "scurvy dog", "whar", "be", "th'", "me", "yer", "galley", "fleabag inn" };

    // list of normal words
    string normal[12] = { "hello", "sir", "madam", "officer", "stranger", "where", "is", "the", "my", "your", "restaurant", "hotel" };

    for (int i = 0; i < s.length(); i++)
    {
        found = s.find(normal[i]);

        if (found > -1)
        {
            string left = s.substr(0, found - 1);   // get left of the string
            string right = s.substr(found + pirate[i].length(), s.length());  // get right of string
            s = left + " " + pirate[i] + " " + right;   // add pirate word in place of normal word
        }
    }

    cout << s;
}

但它并没有真正起作用,而且有很多错误,所以我尝试改用 replace() 函数:

#include<iostream>
#include<string>
#include<conio.h>

using namespace std;

void speakPirate(string s);

int main()
{
    string phrase;

    cout << "Enter the phrase to Pirate-Ize: ";
    getline(cin, phrase);

    speakPirate(phrase);

    _getch();
    return 0;
}

void speakPirate(string s)
{
    int found;

    // list of pirate words
    string pirate[12] = { "ahoy", "matey", "proud beauty", "foul blaggart", "scurvy dog", "whar", "be", "th'", "me", "yer", "galley", "fleabag inn" };

    // list of normal words
    string normal[12] = { "hello", "sir", "madam", "officer", "stranger", "where", "is", "the", "my", "your", "restaurant", "hotel" };

    for (int i = 0; i < s.length(); i++)
    {
        found = s.find(normal[i]);

        if (found > -1)
        {
            s.replace(found, found + pirate[i].length(), pirate[i]);
        }
    }

    cout << s;
}

我不确定为什么,但这也不起作用。我还注意到,当我尝试将一个较大的词更改为一个较小的词时,一些原始词被遗留下来,例如:

    Enter the phrase to Pirate-Ize: hello
    ahoyo

我只是注意到它有时甚至可能根本不会改变这个词,例如:

    Enter the phrase to Pirate-Ize: where
    where

怎么会?有人可以告诉我我需要做什么或我可以实施的更有效的解决方案吗?非常感谢。

此处迭代文本的长度:

for (int i = 0; i < s.length(); i++)

它应该是文本数组的长度,类似于

for (int i = 0; i < 12; i++)

但是,您应该使用 std::map 来模拟普通单词和盗版单词之间的映射。

std::map<std::string, std::string> words = {
  {"hello", "ahoy"},
  // .. and so on
};

for(auto const & kvp : words) 
{
   // replace kvp.first with kvp.second
}

Marius 是正确的,主要错误是您需要遍历数组的长度。与映射不同的方法是在使用 replace() 的地方使用 erase() 和 insert()。 replace() 不考虑字符串的长度不同,但删除一个子字符串然后添加一个新的子字符串会。这可以按如下方式完成

for (int i = 0; i < 12; i++)
{
    found = s.find(normal[i]);

    // Locate the substring to replace
    int pos = s.find( normal[i], found );
    if( pos == string::npos ) break;
    // Replace by erasing and inserting
    s.erase( pos, normal[i].length() );
    s.insert( pos, pirate[i] );
}