#include <stdio.h>
#include <stdlib.h>

int ft_strlen(char *str)
{
    int i = 0;

    while(str[i])
        i++;
    return i;
}

char	*ft_strcat(char *dest, char *src)
{
	int	i;
	int	j;

	i = 0;
	j = 0;
	while (dest[i] != '\0')
		i++;
	while (src[j] != '\0')
	{
		dest[i] = src[j];
		i++;
		j++;
	}
	//dest[i] = '\0';
	return (dest);
}


int for_joinlen(int size, char **strs, char *sep)
{
    int i = 0;
    int len[size];
    int total_len = 0;
    int sep_len;
    
    //total len에 문자열들의 길이, sep의 길이를 size에 맞게 중첩시키고 있음.
    while(i < size){
        len[i] = ft_strlen(strs[i]);
        //printf("%d\n", len[i]); > 5,5,1
        total_len += len[i];
        i++;
    }
    sep_len = ft_strlen(sep);
    sep_len = sep_len*(size-1);
    //printf("%d", sep_len); > 4

    total_len += sep_len;
    //printf("%d", total_len); > 15

    return total_len;
}


char *ft_strjoin(int size, char **strs, char *sep)
{
    int i = 0;

    int total_len = for_joinlen(size, strs, sep);
/*
    //total len에 문자열들의 길이, sep의 길이를 size에 맞게 중첩시키고 있음.
    while(i < size){
        len[i] = ft_strlen(strs[i]);
        //printf("%d\n", len[i]); > 5,5,1
        total_len += len[i];
        i++;
    }
    sep_len = ft_strlen(sep);
    sep_len = sep_len*(size-1);
    //printf("%d", sep_len); > 4

    total_len += sep_len;
    //printf("%d", total_len); > 15
*/

    

    //동적할당, total len의 크기에 맞게 동적할당을 하고 있음
    char *mal = (char *)malloc(sizeof(char) * (total_len + 1));
    if(mal == 0)
        return 0;
    i = 0;
    
    while(i < total_len+1){
        mal[i] = '\0';
        i++;
    }

    //동적할당된 공간에 문자열과 sep, 문자열, sep을 순서대로 배치해야함.
    i=0;
    while (i < size)
    {
        ft_strcat(mal, strs[i]);
        if(i != size-1)
            ft_strcat(mal, sep);
        i++;
    }
    return mal;
}


int main()
{
    
    char *str[] = {"hello", "world", "!", "fucking", "join"};
    char *sep = ", ";

    char *result = ft_strjoin(5, str, sep);
    printf("%s", result);

    if(result!=0)
        free(result);   
    
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: