add: strjoin and strmerger

This commit is contained in:
starnakin 2023-07-01 11:38:53 +02:00
parent 801e5e32e5
commit eabd3070db
3 changed files with 39 additions and 0 deletions

View File

@ -1,4 +1,6 @@
#pragma once
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
@ -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);

14
src/str/str_join.c Normal file
View File

@ -0,0 +1,14 @@
#include "str.h"
#include <string.h>
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;
}

21
src/str/str_merger.c Normal file
View File

@ -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;
}