add: str_replace

This commit is contained in:
starnakin 2023-07-05 14:43:22 +02:00
parent 6d2b0a74c4
commit 698e18ecb4
2 changed files with 20 additions and 0 deletions

View File

@ -10,3 +10,4 @@ void str_shift(char *str, int shift);
int str_contain_only(const char *str, const char *charset);
char* str_merger(size_t arg_len, ...);
char* str_join(const char* s1, const char* s2);
char* str_replace(const char *str, const char *fill, size_t start, size_t stop);

19
src/str/str_replace.c Normal file
View File

@ -0,0 +1,19 @@
#include "./str.h"
char *str_replace(const char *str, const char *fill, size_t start, size_t stop)
{
char *out;
size_t sum;
out = malloc((strlen(str) + strlen(fill) -\
(stop - start) + 1 * sizeof(char)));
if (out == NULL)
return (NULL);
strncpy(out, str, start);
strncpy(out + start, fill, strlen(fill));
sum = start + strlen(fill);
strncpy(out + sum, str + stop, strlen(str) - stop);
out[sum + strlen(str) - stop] = '\0';
return (out);
}