From c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:08:27 +0200 Subject: add: graphs et rushs --- graphs/cpp/replace/replace.cc | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 graphs/cpp/replace/replace.cc (limited to 'graphs/cpp/replace/replace.cc') diff --git a/graphs/cpp/replace/replace.cc b/graphs/cpp/replace/replace.cc new file mode 100644 index 0000000..c24bf05 --- /dev/null +++ b/graphs/cpp/replace/replace.cc @@ -0,0 +1,39 @@ +#include "replace.hh" + +#include +#include +#include + +void replace(const std::string& input_filename, + const std::string& output_filename, const std::string& src_token, + const std::string& dst_token) +{ + std::ofstream file_out; + file_out.open(output_filename); + + if (!file_out.is_open()) + { + std::cerr << "Cannot write output file\n"; + return; + } + + std::ifstream file_in; + file_in.open(input_filename); + if (!file_in.is_open()) + { + std::cerr << "Cannot open input file\n"; + return; + } + + std::string token; + while (std::getline(file_in, token)) + { + std::string::size_type n = 0; + while ((n = token.find(src_token, n)) != std::string::npos) + { + token.replace(n, src_token.size(), dst_token); + n += dst_token.size(); + } + file_out << token << "\n"; + } +} \ No newline at end of file -- cgit v1.2.3