42_C_PISCINE/C/c07v3/ex03/ft_strjoin.c
Camille Chauvet 29ed24d567 init
2023-05-17 16:45:25 +00:00

71 lines
1.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 13:29:37 by cchauvet #+# #+# */
/* Updated: 2022/08/03 14:20:13 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i] != 0)
i++;
return (i);
}
char *ft_strcat(char *dest, char *src)
{
int i;
int j;
j = 0;
i = 0;
while (dest[i] != 0)
i++;
while (src[j] != 0)
{
dest[i + j] = src[j];
j++;
}
dest[i + j] = 0;
return (dest);
}
char *ft_strjoin(int size, char **strs, char *sep)
{
int i;
int word_counter;
char *out;
if (size == 0)
{
out = malloc(sizeof(*out));
out = "";
return (out);
}
i = -1;
while (++i < size)
{
word_counter += ft_strlen(strs[i]);
word_counter += ft_strlen(sep);
}
out = malloc(sizeof(*out) * word_counter);
i = 0;
while (i < size)
{
ft_strcat(out, strs[i++]);
if (i < size)
ft_strcat(out, sep);
}
return (out);
}