ft_sort_params.c
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* ft_sort_params.c :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: lfiorell <lfiorell@student.42nice.fr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2024/07/28 22:29:10 by lfiorell #+# #+# */ 9 /* Updated: 2024/07/28 22:29:11 by lfiorell ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include <unistd.h> 14 15 int string_lenght(char *str) 16 { 17 int i; 18 19 i = 0; 20 while (str[i]) 21 { 22 i++; 23 } 24 return (i); 25 } 26 27 int string_compare(char *str1, char *str2) 28 { 29 while (*str1 && (*str1 == *str2)) 30 { 31 str1++; 32 str2++; 33 } 34 return (*(const unsigned char *)str1 - *(const unsigned char *)str2); 35 } 36 37 // Function to swap two strings 38 void swap(char **str1_ptr, char **str2_ptr) 39 { 40 char *temp; 41 42 temp = *str1_ptr; 43 *str1_ptr = *str2_ptr; 44 *str2_ptr = temp; 45 } 46 47 // Function to sort an array of strings 48 void sort_strings(char *arr[]) 49 { 50 int n; 51 int i; 52 int j; 53 54 n = 0; 55 while (arr[n]) 56 n++; 57 i = 0; 58 while (i < n - 1) 59 { 60 j = 0; 61 while (j < n - i - 1) 62 { 63 if (string_compare(arr[j], arr[j + 1]) > 0) 64 { 65 swap(&arr[j], &arr[j + 1]); 66 } 67 j++; 68 } 69 i++; 70 } 71 } 72 73 int main(int argc, char **argv) 74 { 75 int i; 76 77 if (argc == 2) 78 { 79 write(1, argv[1], string_lenght(argv[1])); 80 write(1, "\n", 1); 81 } 82 else if (argc > 2) 83 { 84 argv++; 85 sort_strings(argv); 86 i = 1; 87 while (i < argc) 88 { 89 write(1, argv[i - 1], string_lenght(argv[i - 1])); 90 write(1, "\n", 1); 91 i++; 92 } 93 } 94 }