From 07b2c4a4d8b546b08cc3d66ad1555e594329d00c Mon Sep 17 00:00:00 2001 From: Joshix Date: Sun, 3 Oct 2021 21:33:53 +0200 Subject: [PATCH] use while loop instead of recursion for replace functions --- uwufetch.c | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/uwufetch.c b/uwufetch.c index 1acf975..2532cca 100644 --- a/uwufetch.c +++ b/uwufetch.c @@ -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()