Skip to content
Closed
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
56 changes: 56 additions & 0 deletions hide-secret.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
int check_and_change(char* const text, const char* const secret,
const int shift)
{
int i = 0;
while (secret[i] != '\0')
{
if (secret[i] != text[i + shift])
{
return shift;
}
i++;
}
const int secret_len = i;


i = 0;
while (i < secret_len && secret[i] != '\0')
{
text[i + shift] = 'x';
i++;
i = check_and_change(text, secret, i + shift) - shift;
}

return shift + i - 1;
}

int get_string_length(const char* const str)
{
int i = 0;
while (str[i] != '\0')
{
i++;
}
return i;
}

void hide_secret(char* const text, const char* const secret)
{
if (!text || !secret)
{
return;
}

const int text_len = get_string_length(text);
const int secret_len = get_string_length(secret);
if (text_len <= 0 || secret_len <= 0 || text_len < secret_len)
{
return;
}

int i = 0;
while (text[i] != '\0')
{
i = check_and_change(text, secret, i) + 1;
}
}