kirill-refactor
This commit is contained in:
35
mine/lab_2/vlad/lib_d.c
Normal file
35
mine/lab_2/vlad/lib_d.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user