Solution to Ex. 06.
authorLukáš Jiřiště <ljiriste@student.42prague.com>
Wed, 28 Jun 2023 05:47:40 +0000 (07:47 +0200)
committerLukáš Jiřiště <ljiriste@student.42prague.com>
Wed, 28 Jun 2023 05:47:40 +0000 (07:47 +0200)
Also contains main.c for additional testing. main.c should not be included in the final commit.

ex06/ft_sort_string_tab.c [new file with mode: 0644]
ex06/main.c [new file with mode: 0644]

diff --git a/ex06/ft_sort_string_tab.c b/ex06/ft_sort_string_tab.c
new file mode 100644 (file)
index 0000000..5c2f09b
--- /dev/null
@@ -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 (file)
index 0000000..bcbd32e
--- /dev/null
@@ -0,0 +1,26 @@
+//28.06.2023 07:29
+
+#include <unistd.h>
+
+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);
+}