70 lines
1.6 KiB
C
70 lines
1.6 KiB
C
|
/* ************************************************************************** */
|
||
|
/* */
|
||
|
/* ::: :::::::: */
|
||
|
/* ft_strjoin.c :+: :+: :+: */
|
||
|
/* +:+ +:+ +:+ */
|
||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||
|
/* +#+#+#+#+#+ +#+ */
|
||
|
/* Created: 2022/07/24 13:29:37 by cchauvet #+# #+# */
|
||
|
/* Updated: 2022/07/24 14:44:52 by cchauvet ### ########.fr */
|
||
|
/* */
|
||
|
/* ************************************************************************** */
|
||
|
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
int ft_strlen(char *str)
|
||
|
{
|
||
|
char i;
|
||
|
|
||
|
i = 0;
|
||
|
while (str[i] != 0)
|
||
|
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 = 0;
|
||
|
while (i < size)
|
||
|
{
|
||
|
word_counter += ft_strlen(strs[i]);
|
||
|
word_counter += ft_strlen(sep);
|
||
|
i++;
|
||
|
}
|
||
|
out = malloc(sizeof(*out) * word_counter);
|
||
|
i = 0;
|
||
|
while (i < size)
|
||
|
{
|
||
|
ft_strcat(out, strs[i++]);
|
||
|
ft_strcat(out, sep);
|
||
|
}
|
||
|
return (out);
|
||
|
}
|