From 26708b8a2aec9000eed0eb5b4586507c613fb63f Mon Sep 17 00:00:00 2001 From: hugodu69 Date: Thu, 12 Dec 2019 02:16:13 +0100 Subject: [PATCH] ecriture du main part2 avance a mapi --- srcs/part2/ft_split.c | 2 +- srcs/part2/ft_strjoin.c | 31 +++++++++++----------------- srcs/part2/main.c | 45 ++++++++++++++++++++++++++++++++--------- 3 files changed, 49 insertions(+), 29 deletions(-) diff --git a/srcs/part2/ft_split.c b/srcs/part2/ft_split.c index 0d1aa9e..40d1c25 100644 --- a/srcs/part2/ft_split.c +++ b/srcs/part2/ft_split.c @@ -104,7 +104,7 @@ char **ft_split(char const *s, char c) { while (s[len] != '\0' && s[len] != c) len++; - if (w == 3 || !(array[w++] = ft_substr(s, 0, len))) + if (!(array[w++] = ft_substr(s, 0, len))) return (ft_free(array, w)); s += len - 1; } diff --git a/srcs/part2/ft_strjoin.c b/srcs/part2/ft_strjoin.c index c9e309c..0dfad46 100644 --- a/srcs/part2/ft_strjoin.c +++ b/srcs/part2/ft_strjoin.c @@ -56,31 +56,24 @@ #include "libft.h" -static char *ft_doit(char const *s1, char const *s2, char *dest) -{ - int j; - int i; - - j = 0; - i = 0; - while (s1[j] != '\0') - dest[i++] = s1[j++]; - j = 0; - while (s2[j] != '\0') - dest[i++] = s2[j++]; - dest[i] = '\0'; - return (dest); -} - char *ft_strjoin(char const *s1, char const *s2) { char *str; + int len; + int i; if (!s1 || !s2) return (NULL); - if (!(str = (char *)malloc(sizeof(char) * - (ft_strlen(s1) + ft_strlen(s2) + 1)))) + len = ft_strlen(s1) + ft_strlen(s2); + if (!(str = (char *)malloc(sizeof(char) * (len + 1)))) return (NULL); - str = ft_doit(s1, s2, str); + len = 0; + i = 0; + while (s1[i] != '\0') + str[len++] = s1[i++]; + i = 0; + while (s2[i] != '\0') + str[len++] = s2[i++]; + str[len] = '\0'; return (str); } diff --git a/srcs/part2/main.c b/srcs/part2/main.c index d4e6997..b520e99 100644 --- a/srcs/part2/main.c +++ b/srcs/part2/main.c @@ -2,20 +2,47 @@ #include +void print_ft_itoa(int i) +{ + printf("%d -> '%s'\n", i, ft_itoa(i)); +} + +void print_ft_split(char *str, char c) +{ + char **tab; + + printf("[%s] [%c] -> ", str, c); + tab = ft_split(str, c); + while (*tab != NULL) + printf("[%s]", *(tab++)); + printf("\n"); +} + +void print_ft_strjoin(char *s1, char *s2) +{ + printf("'%s' + '%s' -> '%s'\n", s1, s2, ft_strjoin(s1, s2)); +} + int main() { - printf("itoa: %s\n", ft_itoa(1338)); + char **tab; + char *str; + char c; + int i; + + printf("itoa:\n"); + print_ft_itoa(1338); + + printf("\nsplit:\n"); + print_ft_split(" dfs zfe f ez f fez ", ' '); + + printf("\nstrjoin:\n"); + print_ft_strjoin("alpha", "bravo"); - char **tab = ft_split(" er era zeraze a", ' '); - int i = 0; - while (tab[i]) - { - write(1, "fg", 2); - printf("split: %s\n", tab[i++]); - } -// printf("strjoin: %s\n", ft_strjoin(1338)); // printf("strmapi: %s\n", ft_strmapi(1338)); + // printf("substr: %s\n", ft_substr(1338)); + // printf("strtrim: %s\n", ft_strtrim(1338)); return 0; }