use while loop instead of recursion for replace functions
This commit is contained in:
parent
cc399b471f
commit
07b2c4a4d8
1 changed files with 15 additions and 22 deletions
15
uwufetch.c
15
uwufetch.c
|
@ -710,36 +710,29 @@ replace("Hello World!", "World", "everyone")
|
||||||
void replace(char *original, char *search, char *replacer)
|
void replace(char *original, char *search, char *replacer)
|
||||||
{
|
{
|
||||||
char *ch;
|
char *ch;
|
||||||
if (!(ch = strstr(original, search)))
|
|
||||||
return;
|
|
||||||
|
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
|
while (ch = strstr(original, search)) {
|
||||||
strncpy(buffer, original, ch - original);
|
strncpy(buffer, original, ch - original);
|
||||||
buffer[ch - original] = 0;
|
buffer[ch - original] = 0;
|
||||||
sprintf(buffer + (ch - original), "%s%s", replacer, ch + strlen(search));
|
sprintf(buffer + (ch - original), "%s%s", replacer, ch + strlen(search));
|
||||||
|
|
||||||
original[0] = 0;
|
original[0] = 0;
|
||||||
strcpy(original, buffer);
|
strcpy(original, buffer);
|
||||||
return replace(original, search, replacer);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void replace_ignorecase(char *original, char *search, char *replacer)
|
void replace_ignorecase(char *original, char *search, char *replacer)
|
||||||
{
|
{
|
||||||
char *ch;
|
char *ch;
|
||||||
if (!(ch = strcasestr(original, search)))
|
|
||||||
return;
|
|
||||||
|
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
|
while (ch = strcasestr(original, search)) {
|
||||||
strncpy(buffer, original, ch - original);
|
strncpy(buffer, original, ch - original);
|
||||||
buffer[ch - original] = 0;
|
buffer[ch - original] = 0;
|
||||||
sprintf(buffer + (ch - original), "%s%s", replacer, ch + strlen(search));
|
sprintf(buffer + (ch - original), "%s%s", replacer, ch + strlen(search));
|
||||||
|
|
||||||
original[0] = 0;
|
original[0] = 0;
|
||||||
strcpy(original, buffer);
|
strcpy(original, buffer);
|
||||||
|
}
|
||||||
return replace_ignorecase(original, search, replacer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_ascii()
|
void print_ascii()
|
||||||
|
|
Loading…
Reference in a new issue