[C] strjoin / strjoin.c / strjoin in c

2022. 9. 14. 10:50🧑🏻‍💻/C & C++

Allocates (with malloc(3)) and returns a new string, 
which is the result of the concatenation of ’s1’ and ’s2’.
char	*ft_strjoin(char const *s1, char const *s2)
{
	char	*result;
	size_t	total_length;

	if (!s1 || !s2)
		return (NULL);
	total_length = (ft_strlen(s1) + ft_strlen(s2) + 1);
	result = (char *)malloc(sizeof(char) * total_length);
	if (!result)
		return (NULL);
	ft_strlcpy(result, s1, total_length);
	ft_strlcat(result, s2, total_length);
	return (result);
}

'🧑🏻‍💻 > C & C++' 카테고리의 다른 글

[C++] Permutation, Combination  (0) 2024.08.10
[C] const  (0) 2022.09.15
[C] substr / substr.c / substr in c  (0) 2022.09.14
[C] memset / memset.c / memset in c  (0) 2022.07.25
[C] memmove / memmove.c / memmove in c  (0) 2022.07.25