libftx/libft/ft_strjoin.c

39 lines
1.3 KiB
C
Raw Permalink Normal View History

2023-01-19 11:33:26 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 11:20:27 by cchauvet #+# #+# */
/* Updated: 2022/09/29 11:30:50 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(const char *s1, const char *s2)
{
char *out;
ssize_t i;
ssize_t j;
size_t len_s1;
size_t len_s2;
if (s1 == NULL || s2 == NULL)
return (NULL);
len_s1 = ft_strlen(s1);
len_s2 = ft_strlen(s2);
out = malloc(sizeof(char) * (len_s1 + len_s2 + 1));
if (out == NULL)
return (NULL);
i = -1;
while (++i < (ssize_t) len_s1)
out[i] = s1[i];
j = -1;
while (++j < (ssize_t) len_s2)
out[j + i] = s2[j];
out[i + j] = 0;
return (out);
}