aboutsummaryrefslogtreecommitdiff
path: root/libft/src/str/ft_strsjoin.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-10-11 15:52:52 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-10-11 15:52:52 +0200
commitc98de126d2252fe47dc2a9094a5f9a8fa6b4b60a (patch)
tree12f1c827ee063ed3e7038f6a704014e611e4f388 /libft/src/str/ft_strsjoin.c
parenta4ceb5974d1b7dcdd12cc81b7eb07893ea16c8ad (diff)
downloadminishell-c98de126d2252fe47dc2a9094a5f9a8fa6b4b60a.tar.gz
minishell-c98de126d2252fe47dc2a9094a5f9a8fa6b4b60a.tar.bz2
minishell-c98de126d2252fe47dc2a9094a5f9a8fa6b4b60a.zip
Removing libft/minishell_test submodules, Removing subject/README/etc
Diffstat (limited to 'libft/src/str/ft_strsjoin.c')
-rw-r--r--libft/src/str/ft_strsjoin.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/libft/src/str/ft_strsjoin.c b/libft/src/str/ft_strsjoin.c
new file mode 100644
index 0000000..0923bde
--- /dev/null
+++ b/libft/src/str/ft_strsjoin.c
@@ -0,0 +1,50 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strsjoin.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/04 14:30:08 by charles #+# #+# */
+/* Updated: 2020/04/04 23:34:30 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_str.h"
+
+/*
+** \brief Join null-terminated array of strings
+** \param strs Array of strings
+** \param delim String iterspersed between strings
+** \return Joined string or NULL on error
+** \note Empty strings are ignored
+*/
+
+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] != '\0' && strs[i + 1] != NULL)
+ ft_strcat(join, delim);
+ }
+ return (join);
+}