Skip to content
Open
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
33 changes: 33 additions & 0 deletions hide-secret.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <cstring>
#include <vector>

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<size_t> indices;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Предлагаю сделать reserve(textlen)


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';
}
}
}