42_solong/libftx/extra/ft_strfjoin.c

43 lines
1.3 KiB
C
Raw Normal View History

2023-01-04 14:07:13 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strfjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 14:06:04 by cchauvet #+# #+# */
2023-01-10 13:04:46 -05:00
/* Updated: 2023/01/10 18:29:59 by cchauvet ### ########.fr */
2023-01-04 14:07:13 -05:00
/* */
/* ************************************************************************** */
#include "extra.h"
char *ft_strfjoin(char *s1, char *s2)
{
2023-01-10 13:04:46 -05:00
ssize_t i;
ssize_t y;
2023-01-04 14:07:13 -05:00
char *out;
2023-01-09 14:41:06 -05:00
out = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
2023-01-04 14:07:13 -05:00
if (out == NULL)
return (NULL);
2023-01-06 13:37:36 -05:00
i = 0;
2023-01-04 14:07:13 -05:00
if (s1 != NULL)
2023-01-10 13:04:46 -05:00
{
i = -1;
while (s1[++i] != '\0')
2023-01-04 14:07:13 -05:00
out[i] = s1[i];
2023-01-10 13:04:46 -05:00
}
2023-01-04 14:07:13 -05:00
free(s1);
y = 0;
2023-01-04 14:07:13 -05:00
if (s2 != NULL)
2023-01-10 13:04:46 -05:00
{
y = -1;
while (s2[++y] != '\0')
2023-01-04 14:07:13 -05:00
out[i + y] = s2[y];
2023-01-10 13:04:46 -05:00
}
2023-01-04 14:07:13 -05:00
free(s2);
out[i + y] = '\0';
return (out);
}