use while loop instead of recursion for replace functions

This commit is contained in:
Joshix 2021-10-03 21:33:53 +02:00
parent cc399b471f
commit 07b2c4a4d8

View file

@ -710,36 +710,29 @@ replace("Hello World!", "World", "everyone")
void replace(char *original, char *search, char *replacer)
{
char *ch;
if (!(ch = strstr(original, search)))
return;
char buffer[1024];
while (ch = strstr(original, search)) {
strncpy(buffer, original, ch - original);
buffer[ch - original] = 0;
sprintf(buffer + (ch - original), "%s%s", replacer, ch + strlen(search));
strncpy(buffer, original, ch - original);
buffer[ch - original] = 0;
sprintf(buffer + (ch - original), "%s%s", replacer, ch + strlen(search));
original[0] = 0;
strcpy(original, buffer);
return replace(original, search, replacer);
original[0] = 0;
strcpy(original, buffer);
}
}
void replace_ignorecase(char *original, char *search, char *replacer)
{
char *ch;
if (!(ch = strcasestr(original, search)))
return;
char buffer[1024];
while (ch = strcasestr(original, search)) {
strncpy(buffer, original, ch - original);
buffer[ch - original] = 0;
sprintf(buffer + (ch - original), "%s%s", replacer, ch + strlen(search));
char buffer[1024];
strncpy(buffer, original, ch - original);
buffer[ch - original] = 0;
sprintf(buffer + (ch - original), "%s%s", replacer, ch + strlen(search));
original[0] = 0;
strcpy(original, buffer);
return replace_ignorecase(original, search, replacer);
original[0] = 0;
strcpy(original, buffer);
}
}
void print_ascii()