piscine/c06/ex03/ft_sort_params.c
Willem Haffmans 607ce08c18 all
2024-09-10 00:18:01 +02:00

81 lines
1.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sort_params.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/12 19:04:43 by whaffman #+# #+# */
/* Updated: 2024/06/16 16:58:42 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putstr(char *str)
{
while (*str != '\0')
{
write(1, str, 1);
str++;
}
}
void ft_swap(char **a, char **b)
{
char *temp;
temp = *a;
*a = *b;
*b = temp;
}
int ft_strcmp(char *s1, char *s2)
{
while (*s1 && *s1 == *s2)
{
s1++;
s2++;
}
return (*s1 - *s2);
}
void ft_sort_strings(char **strings, int size)
{
int i;
int swapped;
swapped = 1;
while (swapped == 1)
{
swapped = 0;
i = 2;
while (i < size)
{
if (ft_strcmp(strings[i - 1], strings[i]) > 0)
{
swapped = 1;
ft_swap(&strings[i - 1], &strings[i]);
}
i++;
}
}
}
int main(int argc, char **argv)
{
int i;
if (argc > 1)
{
ft_sort_strings(argv, argc);
i = 1;
while (i < argc)
{
ft_putstr(argv[i++]);
ft_putstr("\n");
}
}
return (0);
}