ft_strjoin

Subject

FT_STRJOIN (simplified)

NAME
    ft_strjoin -- concatenate 2 strings in a new string
SYNOPSIS
    char *ft_strjoin(const char *s1, const char *s2);
DESCRIPTION
    Allocate (with malloc(3)) and returns a new string resulting from the concatenation of s1 and s2.
PARAMETERS
    s1: prefix string
    s2: suffix string
RETURN VALUES
    ft_strjoin() returns the new string; NULL if the memory allocation failed.
AUTHORIZED EXTERNAL FUNCTIONS
    malloc(3)

Understandable explanation

This function works basically the same way as ft_strlcat does, but instead of passing it a destination string that has to be correctly allocated as a parameter, we only pass two strings and ft_strjoin will allocate the required memory for both of them plus the NUL-terminating character.

s1 will be the first string in the result, s2 the second one.

Hints

We have to get the length of both strings so we can allocate enough memory for both of them.

So that's the first thing to do. Then we can allocate enough memory for both string plus the NUL-terminating character.

We then copy s1 into our newly allocated string, then we copy s2, and finally we can set the last character as NUL.

Commented solution

ft_strjoin

Last updated

Was this helpful?