diff --git a/src/str/str.h b/src/str/str.h index 4689b2a..908712e 100644 --- a/src/str/str.h +++ b/src/str/str.h @@ -1,4 +1,6 @@ #pragma once + +#include #include #include #include @@ -6,3 +8,5 @@ char **split(const char *str, const char *delimiter); 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); diff --git a/src/str/str_join.c b/src/str/str_join.c new file mode 100644 index 0000000..f456175 --- /dev/null +++ b/src/str/str_join.c @@ -0,0 +1,14 @@ +#include "str.h" +#include + +char *str_join(const char *s1, const char *s2) +{ + char *out; + + out = malloc((strlen(s1) + strlen(s2) + 1) * sizeof(char)); + if (out == NULL) + return (NULL); + strcpy(out, s1); + strcat(out, s2); + return out; +} diff --git a/src/str/str_merger.c b/src/str/str_merger.c new file mode 100644 index 0000000..e840a9e --- /dev/null +++ b/src/str/str_merger.c @@ -0,0 +1,21 @@ +#include "str.h" + +char *str_merger(size_t arg_len, ...) +{ + va_list va; + char *out; + char *temp; + + va_start(va, arg_len); + out = str_join(va_arg(va, char *), va_arg(va, char *)); + while (arg_len > 2) + { + temp = str_join(out, va_arg(va, char *)); + free(out); + if (temp == NULL) + return (NULL); + out = temp; + arg_len--; + } + return out; +}