0

Заменить каждую подстроку в тексте на другое слово или символ. Использовать вектор

Я попытался написать программу, но она не работает

#include <iostream>
#include <vector>
#include <string>
#include <numeric>

using namespace std;

int cens(vector <string> text, vector <string> ban)
{
    size_t n = text.size;
    size_t m = ban.size;
    for(size_t i = 0; i < n; i++)
    {
        for (size_t j = 0; j < m; j++)
        {
            if(text[i] == ban[j])
            {
                text[i] = "***";
                j = 0;
            } 
        }
    }
    return text;
}

void test()
{
    vector <string> text =
    {
        "I begin my work with the time when Servius Galba was consul for the second time with Titus Vinius for his colleague.", 
        "Of the former period, the 820 years dating from the founding of the city, many authors have treated;", 
        "and while they had to record the transactions of the Roman people, they wrote with equal eloquence and freedom.", 
        "After the conflict at Actium, and when it became essential to peace, that all power should be centered in one man, these great intellects passed away." 
    }
    vector <string> ban =
    {
        "beg", 
        "aut", 
        "record",
        "zor" 
    }
    cout << cens(text, ban) << endl;
}

int main()
{
    test();
    return 0;
}

1 ответ 1

2

Замену подстроки надо осуществлть через поиск подстроки. Есть много алгоритмов такой которые выполняют эту задачу.

Вот пример решения в лоб:

#include <string>
#include <iostream>
#include <vector>

void ReplaceString(std::string& subject, const std::string& search, const std::string& replace, bool word_match = false) {
  std::size_t pos = 0;
  while ((pos = subject.find(search, pos)) != std::string::npos) {
    if (word_match && pos < subject.size() && subject[pos + 1] != ' ') {
      pos += search.length();
      continue;
    }
    subject.replace(pos, search.length(), replace);
    pos += replace.length();
  }
}

void test() {
  std::string text{
        "I begin my work with the time when Servius Galba was consul for the second time with Titus Vinius for his colleague."
        "Of the former period, the 820 years dating from the founding of the city, many authors have treated;"
        "and while they had to record the transactions of the Roman people, they wrote with equal eloquence and freedom."
        "After the conflict at Actium, and when it became essential to peace, that all power should be centered in one man, these great intellects passed away." };
  std::cout << text << std::endl;
  std::vector<std::string> ban{"beg", "aut", "record", "zor", "way"};

  for (const auto& w : ban) {
    ReplaceString(text, w, "***", false);
  }

  std::cout << text << std::endl;
}

int main() {
  test();

  return 0;
}

Ваш ответ

Нажимая «Отправить ответ», вы соглашаетесь с условиями пользования и подтверждаете, что прочитали политику конфиденциальности.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.