ft_strace/libft/ft_strjoin.c

42 lines
1.3 KiB
C
Executable file

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:14:45 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:04:47 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
//returns NULL if any argument is null or if an allocation fails
char *ft_strjoin(char const *s1, char const *s2)
{
char *ret;
int i;
int j;
if (!s1 || !s2)
return (NULL);
ret = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (!ret)
return (NULL);
i = 0;
while (s1[i])
{
ret[i] = s1[i];
i++;
}
j = 0;
while (s2[j])
{
ret[i] = s2[j];
i++;
j++;
}
ret[i] = '\0';
return (ret);
}