aboutsummaryrefslogtreecommitdiff
path: root/src/str/ft_strsjoin.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/str/ft_strsjoin.c')
-rw-r--r--src/str/ft_strsjoin.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/str/ft_strsjoin.c b/src/str/ft_strsjoin.c
new file mode 100644
index 0000000..507903b
--- /dev/null
+++ b/src/str/ft_strsjoin.c
@@ -0,0 +1,42 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strsjoin.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/04 14:30:08 by charles #+# #+# */
+/* Updated: 2020/04/04 14:45:58 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_str.h"
+
+char *ft_strsjoin(char **strs, char *delim)
+{
+ int i;
+ size_t join_len;
+ size_t delim_len;
+ char *join;
+
+ delim_len = ft_strlen(delim);
+ join_len = 0;
+ i = -1;
+ while (strs[++i] != NULL)
+ {
+ join_len += ft_strlen(strs[i]);
+ if (strs[i + 1] != NULL)
+ join_len += delim_len;
+ }
+ if ((join = (char*)malloc(sizeof(char) * (join_len + 1))) == NULL)
+ return (NULL);
+ join[0] = '\0';
+ i = -1;
+ while (strs[++i] != NULL)
+ {
+ ft_strcat(join, strs[i]);
+ if (strs[i + 1] != NULL)
+ ft_strcat(join, delim);
+ }
+ return (join);
+}