Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions hide-secret.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <cstddef>
#include <cstring>
#include <vector>

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<size_t> 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;
}
}
}
1 change: 0 additions & 1 deletion hide-secret.hpp
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
void hide_secret(char* const text, const char* const secret);