piscine-c08/ex04/ft_strs_to_tab.c

79 lines
2.2 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strs_to_tab.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/31 00:56:37 by tosuman #+# #+# */
/* Updated: 2023/03/31 00:56:38 by tosuman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "ft_stock_str.h"
int ft_strlen(char *str)
{
int size;
size = 0;
while (*str++)
++size;
return (size);
}
char *ft_strcpy(char *str)
{
char *copy;
int idx;
copy = malloc(sizeof(char) * (ft_strlen(str) + 1));
idx = 0;
while (*str)
copy[idx++] = *str++;
copy[idx] = 0;
return (copy);
}
struct s_stock_str *ft_strs_to_tab(int ac, char **av)
{
int idx;
t_stock_str *tab;
tab = malloc(sizeof(t_stock_str) * (ac + 1));
idx = -1;
while (++idx < ac)
{
tab[idx].size = ft_strlen(av[idx]);
tab[idx].str = av[idx];
tab[idx].copy = ft_strcpy(av[idx]);
}
tab[idx].str = 0;
return (tab);
}
/* ////
#include <stdio.h>
int main(void)
{
char *strs[4];
t_stock_str *tab;
strs[0] = "First";
strs[1] = "Second";
strs[2] = "Third";
tab = ft_strs_to_tab(3, strs);
printf("tab[0] element size:%d, str:'%s' (%p), copy:'%s' (%p)\n",
tab[0].size, tab[0].str, tab[0].str, tab[0].copy, tab[0].copy);
printf("tab[1] element size:%d, str:'%s' (%p), copy:'%s' (%p)\n",
tab[1].size, tab[1].str, tab[1].str, tab[1].copy, tab[1].copy);
printf("tab[2] element size:%d, str:'%s' (%p), copy:'%s' (%p)\n",
tab[2].size, tab[2].str, tab[2].str, tab[2].copy, tab[2].copy);
printf("tab[3] element size:%d, str:'%s' (%p), copy:'%s' (%p)\n",
tab[3].size, tab[3].str, tab[3].str, tab[3].copy, tab[3].copy);
return (0);
}
*/ ////