Libft
42 Libft library documentation
Loading...
Searching...
No Matches
ft_strjoin.c File Reference
+ Include dependency graph for ft_strjoin.c:

Go to the source code of this file.

Functions

char * ft_strjoin (char const *s1, char const *s2)
 Concatenates two strings into a newly allocated string.
 

Function Documentation

◆ ft_strjoin()

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

Concatenates two strings into a newly allocated string.

Parameters
s1First input string.
s2Second input string.
Returns
Newly allocated concatenation, or NULL on failure.

Definition at line 22 of file ft_strjoin.c.

23{
24 char *join;
25 size_t len1;
26 size_t len2;
27
28 if (!s1 || !s2)
29 return (NULL);
30 len1 = ft_strlen(s1);
31 len2 = ft_strlen(s2);
32 join = (char *)malloc(sizeof(char) * (len1 + len2 + 1));
33 if (!join)
34 return (NULL);
35 ft_memcpy(join, s1, len1);
36 ft_memcpy(join + len1, s2, len2);
37 join[len1 + len2] = '\0';
38 return (join);
39}
void * ft_memcpy(void *dest, const void *src, size_t n)
Copies bytes from source to destination.
Definition ft_memcpy.c:25
size_t ft_strlen(const char *str)
Returns the length of a null-terminated string.
Definition ft_strlen.c:21

References ft_memcpy(), and ft_strlen().

+ Here is the call graph for this function: