diff --git a/hide-secret.cpp b/hide-secret.cpp new file mode 100755 index 0000000..ea09801 --- /dev/null +++ b/hide-secret.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +void hide_secret(char* const text, const char* const secret) { + if (!text || !secret) { return; } + + const unsigned textlen = strlen(text); + const unsigned secretlen = strlen(secret); + + if (textlen < 1 || secretlen < 1 || textlen < secretlen) { return; } + + std::vector indices; + + for (size_t i = 0; i < textlen - secretlen + 1; i++) { + bool isSubstring = true; + + for (size_t j = 0; j < secretlen; j++) { + if (text[i+j] != secret[j]) { + isSubstring = false; + break; + } + } + + if (isSubstring) { indices.push_back(i); } + } + + for (size_t index : indices) { + for (size_t j = 0; j < secretlen; j++) { + text[index + j] = 'x'; + } + } +}