36 lines
894 B
C
36 lines
894 B
C
#include <stddef.h>
|
|
|
|
int replace_char(char *buf, int key, int *replacements_left) {
|
|
(void)key;
|
|
int replaced = 0;
|
|
size_t i = 0;
|
|
|
|
while (buf[i] != '\0') {
|
|
if (buf[i] == '\n') {
|
|
i++;
|
|
continue;
|
|
}
|
|
|
|
// Detect start of a symbol run
|
|
char symbol = buf[i];
|
|
size_t run_start = i;
|
|
size_t run_len = 1;
|
|
|
|
// Count length of run
|
|
while (buf[i + run_len] == symbol && buf[i + run_len] != '\n' && buf[i + run_len] != '\0') {
|
|
run_len++;
|
|
}
|
|
|
|
// For pairs and longer runs: replace every 2nd, 4th, ...
|
|
for (size_t j = 1; j < run_len && *replacements_left > 0; j += 2) {
|
|
buf[run_start + j] = ' ';
|
|
replaced++;
|
|
(*replacements_left)--;
|
|
if (*replacements_left == 0) break;
|
|
}
|
|
|
|
i += run_len;
|
|
}
|
|
return replaced;
|
|
}
|