42_libft/ft_strjoin.c

39 lines
1.3 KiB
C
Raw Permalink Normal View History

2022-09-27 11:25:41 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
2022-10-04 08:21:55 -04:00
/* ft_strjoin.c :+: :+: :+: */
2022-09-27 11:25:41 -04:00
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
2022-10-04 08:21:55 -04:00
/* Created: 2022/09/29 11:20:27 by cchauvet #+# #+# */
/* Updated: 2022/09/29 11:30:50 by cchauvet ### ########.fr */
2022-09-27 11:25:41 -04:00
/* */
/* ************************************************************************** */
#include "libft.h"
2022-10-04 08:21:55 -04:00
char *ft_strjoin(const char *s1, const char *s2)
2022-09-27 11:25:41 -04:00
{
2022-10-04 08:21:55 -04:00
char *out;
ssize_t i;
ssize_t j;
size_t len_s1;
size_t len_s2;
2022-09-27 11:25:41 -04:00
2022-10-04 08:21:55 -04:00
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);
2022-09-27 11:25:41 -04:00
}