#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <random>
std::string replace_random_word(const std::string& text,
const std::vector<std::string>& replacements)
{
// Split text into words
std::stringstream ss(text);
std::vector<std::string> words;
std::string word;
while (ss >> word) {
words.push_back(word);
}
if (words.empty() || replacements.empty()) {
return text; // nothing to do
}
// Random engine
static std::random_device rd;
static std::mt19937 gen(rd());
// Pick random index in the sentence
std::uniform_int_distribution<> dist_word(0, words.size() - 1);
int idx = dist_word(gen);
// Pick random replacement word
std::uniform_int_distribution<> dist_repl(0, replacements.size() - 1);
words[idx] = replacements[dist_repl(gen)];
// Rebuild the string
std::ostringstream out;
for (size_t i = 0; i < words.size(); ++i) {
if (i > 0) out << " ";
out << words[i];
}
return out.str();
}
int main() {
std::string text = "The quick brown fox jumps over the lazy dog";
std::vector<std::string> replacement_words = {"c#", "c++", "java", "rust", "python"};
std::string result = replace_random_word(text, replacement_words);
std::cout << result << "\n";
return 0;
}
/*
run:
The quick brown fox jumps over rust lazy dog
*/