I am struggling with a an exercise, I'm currently doing a pool in an affiliate school with 42 called 1337 (In case you're curious about why).
- I am supposed to write a program that displays arguments given in the command line sorted by ascii order.
- I should display all arguments, except argv[0].
- Every argument should be in it's own line (Basically put a \n).
The use of printf is definitely forbidden and is considered as cheat, the only function I'm allowed to use is write(), from the unistd.h library #include
I've done other exercises, that I'm gonna link to you, one that displays arguments normally, and one that displays them in reverse order.
The code below literally just prints the arguments
#include <unistd.h>
void ft_putchar(char ch)
{
write(1, &ch, 1);
}
void ft_print_params(int argc, char *argv)
{
int i;
i = 0;
while (i < argc)
{
while (argv[i] != '\0')
{
ft_putchar(argv[i]);
i++;
}
i++;
}
}
int main(int argc, char **argv)
{
int i;
i = 1;
while (i < argc)
{
ft_print_params(argc, argv[i]);
ft_putchar('\n');
i++;
}
return (0);
}
The program below, prints the arguments in reverse order.
#include <unistd.h>
void ft_putchar(char ch)
{
write(1, &ch, 1);
}
void ft_print_params(char *argv)
{
int i;
i = 0;
while (argv[i] != '\0')
{
ft_putchar(argv[i]);
i++;
}
}
int main(int argc, char **argv)
{
int i;
i = argc;
if (1)
{
while (i > 1)
{
ft_print_params(argv[i - 1]);
ft_putchar('\n');
i--;
}
}
return (0);
}
So I would love if someone could help me out, with either ideas, or maybe some code, and with some explanations if possible.
Thanks.
qsort()?argvis: An array of strings (well it's an array of pointers but you can see it as an array of strings).argctoft_print_params?