Untitled

 avatar
unknown
plain_text
17 days ago
3.5 kB
4
Indexable
#include <stdlib.h>

int    ft_strlen(char *str)
{
        int    i;

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

int    len(char **str)
{
        int    i;
            int    x;
                int    count;

                    i = 0;
                        x = 0;
                            count = 0;
                                while (str[i] != NULL)
                                    {
                                                while (str[i][x])
                                                        {
                                                                        x++;
                                                                                    count++;
                                                        }
                                                                x = 0;
                                                                        i++;
                                    }
                                        return (count);
}

void    fill_join(char *join, int size, char **strs, char *sep)
{
        int    i;
            int    x;
                int    y;
                    int    z;

                        i = 0;
                            y = 0;
                                while (strs[i] != NULL)
                                    {
                                                x = 0;
                                                        while (strs[i][x])
                                                                    join[y++] = strs[i][x++];
                                                                            z = 0;
                                                                                    while (sep[z] && i < size - 1)
                                                                                                join[y++] = sep[z++];
                                                                                                        i++;
                                    }
                                        join[y] = '\0';
}

char    *ft_strjoin(int size, char **strs, char *sep)
{
        char    *join;

            join = malloc(len(strs) + (ft_strlen(sep) * (size - 1)) + 1 * sizeof(char));
                if (join == NULL)
                        return (NULL);
                            if (size == 0)
                                    return (join);
                                        /*if (size == 0)
                                            {
                                                        join = (char *)malloc(sizeof(char));
                                                                join[0] = '\0';
                                                                        return (join);
                                            }*/
                                                fill_join(join, size, strs, sep);
                                                    return (join);
}

#include <stdio.h>
int main()
{
        char *strs[] = {"Hello", "Jacques", "Ceci", "Est", "Un" , NULL};
            char sep[] = " !";
                char *x = ft_strjoin(5, strs, sep);
                    printf("%s\n", x);
                        free(x);
                            return 0;
}
}
                                            }
}
                                    }
}
                                                        }
                                    }
}
}
Editor is loading...
Leave a Comment