2

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.

8
  • 2
    Are you allowed to use qsort()? Commented Jul 18, 2019 at 16:19
  • Do you know how to sort an array of strings? Then you're all set, because that's all argv is: An array of strings (well it's an array of pointers but you can see it as an array of strings). Commented Jul 18, 2019 at 16:20
  • The two versions of code you posted look identical to me. Why are you passing argc to ft_print_params? Commented Jul 18, 2019 at 16:22
  • @Shawn No I am not, allowed to use it, the only function I'm allowed to use is a write, for the printing nothing else. Commented Jul 18, 2019 at 16:38
  • @Someprogrammerdude Yes I do know how to do that, but i'm kinda confused. Commented Jul 18, 2019 at 16:39

2 Answers 2

3

You could write a function to perform a simple insertion sort on an array of char * in situ:

#include <string.h>

void ft_sort_strings(int num, char **s)
{
    int i, j;
    for (i = 1; i < num; i++)
    {
        for (j = i; j > 0 && strcmp(s[j-1], s[j]) > 0; j--)
        {
            char *temp = s[j-1];
            s[j-1] = s[j];
            s[j] = temp;
        }
    }
}

Then, call it from main to sort argv, skipping the first element:

    ft_sort_strings(argc - 1, argv + 1);

Then you just need to loop through the arguments and print them as you did before.

If you are not allowed to use strcmp from the standard library, it is easy enough to replace it with your own ft_strcmp:

int ft_strcmp(const char *a, const char *b)
{
    unsigned char ac, bc;
    /* interpret chars as unsigned char for compatibility with strcmp() */
    while (ac = *(const unsigned char *)a,
           bc = *(const unsigned char *)b,
           ac && ac == bc)
    {
        a++;
        b++;
    }
    return (ac > bc) - (ac < bc);
}
Sign up to request clarification or add additional context in comments.

Comments

0

So you've actually done the hardest part already, figuring out argc argv in the two first exercises.
Here, you need to do 2 things (more that you are not during in first and second exercise)

  1. Need to compare the ascii value of the strings. Now, remember C02 ? Yes that's correct, you did already write your string compare function.
    If not, you may want to use man strcmp and try to redo your own ft_strcmp (it is not that hard).
    Or, you may use the already existing strncmp function in the C library #include <string.h>

  2. Next you need to do a swap : you need to swap argv with argv+1 if the comparison in between those two is bigger than one. In order to make your swap working you need a temp buffer to store temporally the value being swapped.

int     ft_strcmp(unsigned char *s1, unsigned char *s2)
  {
          int     i;
  
          i = 0;
          while (s1[i] || s2[i])
          {
                  if (s1[i] != s2[i])
                          return (s1[i] - s2[i]);
                  i++;
          }
          return (s1[i] - s2[i]);
  }
  
  void    swap(char **s1, char **s2)
  {
          char    *temp;
  
          temp = *s2;
          *s2 = *s1;
          *s1 = temp;
  }
  
  int     main(int argc, char *argv[])
  {
          int     i;
  
          i = 1;
          if (argc >= 2)
          {
                  while (++i < argc - 1)
                  {
                          if (ft_strcmp(argv[i], argv[i+1]) > 0)
                          {
                                  swap(&argv[i], &argv[i + 1]);
                                  i = 0;
                          }
                  } 
                  i = 0;
                  while (++i < argc )
                          ft_putstr(argv[i]);
          }
          else
                  ft_putchar('\n');
          return(0);
  }

3 Comments

Note that return (s1[i] - s2[i]); returns the wrong sign when the signs of s1[i] and s2[i] differ. str...() functions perform as if characters were unsigned char. "For all functions in this subclause, each character shall be interpreted as if it had the type unsigned char".
Tip: improve code by removing lines numbers.
Please remove the line numbers in your code, keep to just answering the question, don't insult the asker, remove salutations, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.