From: Lukáš Jiřiště Date: Wed, 28 Jun 2023 05:47:40 +0000 (+0200) Subject: Solution to Ex. 06. X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=f2f0f0f27babcbaffdcf976585887022c938e210;p=42%2FC_11.git Solution to Ex. 06. Also contains main.c for additional testing. main.c should not be included in the final commit. --- diff --git a/ex06/ft_sort_string_tab.c b/ex06/ft_sort_string_tab.c new file mode 100644 index 0000000..5c2f09b --- /dev/null +++ b/ex06/ft_sort_string_tab.c @@ -0,0 +1,43 @@ +// 28.06.2023 07:13 + +void ft_swap(char **s1, char **s2) +{ + char *temp; + + temp = *s1; + *s1 = *s2; + *s2 = temp; + return ; +} + +int ft_strcmp(char *s1, char *s2) +{ + while (*s1 == *s2 && *s1 && *s2) + { + ++s1; + ++s2; + } + return (*s1 - *s2); +} + +void ft_sort_string_tab(char **tab) +{ + int i; + int j; + + if (!*tab) + return ; + i = 0; + while (tab[i + 1]) + { + j = i + 1; + while (tab[j]) + { + if (ft_strcmp(tab[i], tab[j]) > 0) + ft_swap(tab + i, tab + j); + ++j; + } + ++i; + } + return ; +} diff --git a/ex06/main.c b/ex06/main.c new file mode 100644 index 0000000..bcbd32e --- /dev/null +++ b/ex06/main.c @@ -0,0 +1,26 @@ +//28.06.2023 07:29 + +#include + +void ft_sort_string_tab(char **tab); + +void print_str_arr(char **strs) +{ + while (*strs) + { + while (**strs) + write(1, (*strs)++, 1); + write(1, "\n", 1); + ++strs; + } + return ; +} + +int main(int argc, char **argv) +{ + if (argc == 1) + return (0); + ft_sort_string_tab(argv + 1); + print_str_arr(argv + 1); + return (0); +}