mirror of
https://gitlab.com/80486DX2-66/gists
synced 2025-01-10 17:32:05 +05:30
str_replace.*: fix the count bug, add a constant
This commit is contained in:
parent
e0d686f033
commit
a61e910a59
@ -28,7 +28,7 @@ char* str_replace(
|
|||||||
|
|
||||||
// count the number of occurrences of the substring
|
// count the number of occurrences of the substring
|
||||||
for (; (p = strstr(p, substr)) != NULL; count++) {
|
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;
|
break;
|
||||||
p += substr_len;
|
p += substr_len;
|
||||||
}
|
}
|
||||||
@ -66,7 +66,7 @@ int main(void) {
|
|||||||
* replacement1 = "universe",
|
* replacement1 = "universe",
|
||||||
* replacement2 = "_____";
|
* 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);
|
* result2 = str_replace(str, substr, replacement2, 1);
|
||||||
|
|
||||||
puts(result1); free(result1);
|
puts(result1); free(result1);
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#define STR_REPLACE_ALL 0
|
||||||
|
|
||||||
char* str_replace(
|
char* str_replace(
|
||||||
const char* str,
|
const char* str,
|
||||||
const char* substr,
|
const char* substr,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user