diff --git a/hide-secret.cpp b/hide-secret.cpp new file mode 100644 index 0000000..bd9eb05 --- /dev/null +++ b/hide-secret.cpp @@ -0,0 +1,41 @@ +#include +#include +#include + +const char kSecretReplacer = 'x'; + +void hide_secret(char* const text, const char* const secret) { + if (text == nullptr || secret == nullptr) { + return; + } + + auto text_len = std::strlen(text); + auto secret_len = std::strlen(secret); + + if (text_len == 0 || secret_len == 0 || text_len < secret_len) { + return; + } + + std::vector secret_start_indices{}; + for (size_t i = 0; i < text_len - secret_len + 1; ++i) { + auto found_match = true; + + for (size_t j = 0; j < secret_len; ++j) { + if (text[i+j] != secret[j]) { + found_match = false; + break; + } + } + if (!found_match) { + continue; + } + + secret_start_indices.push_back(i); + } + + for (const auto idx : secret_start_indices) { + for (size_t i = idx; i < idx + secret_len; ++i) { + text[i] = kSecretReplacer; + } + } +} diff --git a/hide-secret.hpp b/hide-secret.hpp index b959198..a3589bd 100644 --- a/hide-secret.hpp +++ b/hide-secret.hpp @@ -1,2 +1 @@ void hide_secret(char* const text, const char* const secret); -