ft_strjoin

one·2021년 1월 22일
0

✅strjoin

  • Allocates (with malloc(3)) and returns a new string, which is the result of the concatenation
    of s1 and s2.
  • s1s2를 연결한 새 문자열 반환

💾Prototype

char *ft_strjoin(char const *s1, char const *s2);

💻Parameters

  • s1 : The prefix string.
  • s2 : The suffix string.

💻Return value

  • The new string. NULL if the allocation fails.

💾Code

#include "libft.h"

char* ft_strjoin(char const* s1, char const* s2)
{
	char* str;
	size_t	s1_len;
	size_t	s2_len;

	s1_len = ft_strlen(s1);
	s2_len = ft_strlen(s2);
	if (!s1 || !s2)
		return (NULL);
	if (!(str = (char*)malloc(sizeof(char) * (s1_len + s2_len + 1))))
		return (0);
	ft_strlcpy(str, s1, s1_len + 1);
	ft_strlcat(str + (s1_len), s2, s2_len + 1);
	return (str);
}

💾Example

int main()
{
	char s1[] = "peanut";
	char s2[] = "butter";

	printf("%s\n", ft_strjoin(s1, s2));

	return 0;
}

profile
늘 호기심을 갖고, 새로운 것에 도전할 것.

0개의 댓글