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,