Seluj78/Minishell

View on GitHub
libft/ft_strjoin_sep.c

Summary

Maintainability
Test Coverage
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_strjoin_sep.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jlasne <marvin@42.fr>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/03/03 11:51:53 by jlasne            #+#    #+#             */
/*   Updated: 2017/03/03 11:52:15 by jlasne           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "includes/libft.h"

char        *ft_strjoin_sep(char *s1, char *sep, char *s2)
{
    char    *str;
    int        len;
    int        i;

    i = -1;
    len = ft_strlen((char*)s1) + ft_strlen((char*)s2) + ft_strlen(sep) + 1;
    if (!(str = (char*)malloc(sizeof(char) * len)))
        return (NULL);
    while (*s1)
    {
        str[++i] = *s1;
        s1++;
    }
    while (*sep)
    {
        str[++i] = *sep;
        sep++;
    }
    while (*s2)
    {
        str[++i] = *s2;
        s2++;
    }
    str[++i] = '\0';
    return (str);
}