Это моя попытка
#include <stdio.h>
void replacemarks(char *dst, const char *src, char c, const char *r) {
while (1) {
while (*src && (*src != c)) *dst++ = *src++;
if (*src == 0) break;
*dst++ = *src++;
if (*src == 0) break;
const char *cc = strchr(src + 1, c);
if (cc) {
const char *rr = r;
while (*rr) *dst++ = *rr++;
src = cc;
*dst++ = *src++;
} else {
while (*src) *dst++ = *src++;
break;
}
}
*dst = 0;
}
int main(void) {
char line[100];
while (fgets(line, sizeof line, stdin)) {
char src[100], c, r[100], dst[100];
sscanf(line, "%s %c%s", src, &c, r);
replacemarks(dst, src, c, r);
printf("replacemarks(..., \"%s\", '%c', \"%s\") ==> \"%s\"\n",
src, c, r, dst);
}
return 0;
}
См. https://ideone.com/LhJaaq
Пример запуска с входом
hello_from_here _ 21223
this_is_name _ 000044
abracadabra r foo
*one*two*three* * ----
*one*two*three*four* * ----
*one*two*three*four * ----
one*two*three*four* * ----
*one*two*three*four*five* * ----
*one*two*three*four*five * ----
one*two*three*four*five* * ----
Выход (украшен для Переполнение стека):
replacemarks(..., "hello_from_here", '_', "21223") ==> "hello_21223_here"
replacemarks(..., "this_is_name", '_', "000044") ==> "this_000044_name"
replacemarks(..., "abracadabra", 'r', "foo") ==> "abrfoora"
replacemarks(..., "*one*two*three*", '*', "----") ==> "*----*two*----*"
replacemarks(..., "*one*two*three*four*", '*', "----") ==> "*----*two*----*four*"
replacemarks(..., "*one*two*three*four", '*', "----") ==> "*----*two*----*four"
replacemarks(..., "one*two*three*four*", '*', "----") ==> "one*----*three*----*"
replacemarks(..., "*one*two*three*four*five*", '*', "----") ==> "*----*two*----*four*----*"
replacemarks(..., "*one*two*three*four*five", '*', "----") ==> "*----*two*----*four*five"
replacemarks(..., "one*two*three*four*five*", '*', "----") ==> "one*----*three*----*five*"