替换句子中的单词?

时间:2015-02-24 21:16:10

标签: c++ string 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)
        {
            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

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

2 个答案:

答案 0 :(得分:2)

在这里迭代文本的长度:

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
}

答案 1 :(得分:1)

Marius是正确的,主要的错误是你需要迭代数组的长度。与映射不同的方法是使用erase()和insert(),其中使用了replace()。 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] );
}