fork(1) download
  1. #include <stdio.h>
  2.  
  3. void replacemarks(char *dst, const char *src, char c, const char *r) {
  4. while (1) {
  5. while (*src && (*src != c)) *dst++ = *src++;
  6. if (*src == 0) break;
  7. *dst++ = *src++;
  8. if (*src == 0) break;
  9. const char *cc = strchr(src + 1, c);
  10. if (cc) {
  11. const char *rr = r;
  12. while (*rr) *dst++ = *rr++;
  13. src = cc;
  14. *dst++ = *src++;
  15. } else {
  16. while (*src) *dst++ = *src++;
  17. break;
  18. }
  19. }
  20. *dst = 0;
  21. }
  22.  
  23. int main(void) {
  24. char line[100];
  25. while (fgets(line, sizeof line, stdin)) {
  26. char src[100], c, r[100], dst[100];
  27. sscanf(line, "%s %c%s", src, &c, r);
  28. replacemarks(dst, src, c, r);
  29. printf("replacemarks(..., \"%s\", '%c', \"%s\") ==> \"%s\"\n",
  30. src, c, r, dst);
  31. }
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 4536KB
stdin
hello_from_here _ 21223
this_is_name _ 000044
abracadabra r foo
one * ----
*one * ----
one* * ----
one*two*three * ----
one*two* * ----
*two*three * ----
*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* * ----
stdout
replacemarks(..., "hello_from_here", '_', "21223") ==> "hello_21223_here"
replacemarks(..., "this_is_name", '_', "000044") ==> "this_000044_name"
replacemarks(..., "abracadabra", 'r', "foo") ==> "abrfoora"
replacemarks(..., "one", '*', "----") ==> "one"
replacemarks(..., "*one", '*', "----") ==> "*one"
replacemarks(..., "one*", '*', "----") ==> "one*"
replacemarks(..., "one*two*three", '*', "----") ==> "one*----*three"
replacemarks(..., "one*two*", '*', "----") ==> "one*----*"
replacemarks(..., "*two*three", '*', "----") ==> "*----*three"
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*"