From c1bfc98c34a6729896ddc0ebc88476c4568ed3bd Mon Sep 17 00:00:00 2001 From: boboboba <95755392+boboboba@users.noreply.github.com> Date: Wed, 29 Oct 2025 01:37:03 +0500 Subject: [PATCH] hide-secret --- hide-secret.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 hide-secret.cpp diff --git a/hide-secret.cpp b/hide-secret.cpp new file mode 100644 index 0000000..2255b10 --- /dev/null +++ b/hide-secret.cpp @@ -0,0 +1,56 @@ +#include + +void hide_secret(char* const text, const char* const secret) +{ + if (text == nullptr || secret == nullptr) + return; + int secretLength = strlen(secret); + if ( secretLength == 0) + return; + for (int i = 0; text[i] != '\0'; i++) + { + bool matched = true; + bool needEnd = false; + for (int j = i; j - i < secretLength; j++) + { + if (text[j] == '\0') + { + needEnd = true; + } + if (text[j] != secret[j - i]) + { + matched = false; + break; + } + } + + if (!matched) + { + continue; + } + if (needEnd) + { + break; + } + text[i] = '\x01'; + } + + int lastStart = -1; + for (int i = 0; text[i] != '\0'; i++) + { + if (text[i] == '\x01') + { + lastStart = i; + } + + if (lastStart == -1) + { + continue; + } + + if (i - lastStart < secretLength) + { + text[i] = 'x'; + } + } +}