From a61e910a59297b916581fd3fd7d7d828b4349a8c Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Sat, 16 Mar 2024 15:24:22 +0300 Subject: [PATCH] str_replace.*: fix the count bug, add a constant --- c-programming/strings/str_replace.c | 4 ++-- c-programming/strings/str_replace.h | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/c-programming/strings/str_replace.c b/c-programming/strings/str_replace.c index a663e6b..8db64e0 100644 --- a/c-programming/strings/str_replace.c +++ b/c-programming/strings/str_replace.c @@ -28,7 +28,7 @@ char* str_replace( // count the number of occurrences of the substring for (; (p = strstr(p, substr)) != NULL; count++) { - if (max_count >= 0 && count >= (size_t) max_count) + if (max_count > 0 && count >= (size_t) max_count) break; p += substr_len; } @@ -66,7 +66,7 @@ int main(void) { * replacement1 = "universe", * replacement2 = "_____"; - char* result1 = str_replace(str, substr, replacement1, -1), + char* result1 = str_replace(str, substr, replacement1, STR_REPLACE_ALL), * result2 = str_replace(str, substr, replacement2, 1); puts(result1); free(result1); diff --git a/c-programming/strings/str_replace.h b/c-programming/strings/str_replace.h index a7fde95..a372ac3 100644 --- a/c-programming/strings/str_replace.h +++ b/c-programming/strings/str_replace.h @@ -8,6 +8,8 @@ #include #include +#define STR_REPLACE_ALL 0 + char* str_replace( const char* str, const char* substr,