2

I need to sort n number of strings lexicographically that are arguments of a function with variable number of arguments. In main function, strings are read as command line arguments.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

void sort(int n,...)
{
    //store optional arguments (strings) to array arr
    va_list args;
    va_start(args,n);
    char **arr=malloc(n * sizeof(char*));
    int i;
    for(i=0;i<n;i++)
        arr[i]=malloc((strlen(va_arg(args,char*)) + 1) * sizeof(char));
    va_end(args);

    //store length of each string to lenArr
    //find max length and store it to temp (for sorting)
    va_list args;
    va_start(args,n);
    int *lenArr=calloc(n , sizeof(int));
    for(i=0;i<n;i++)
    {
        lenArr[i]=strlen(va_arg(args,char*)) + 1;
    }

    int max=0;
    for(i=0;i<n;i++)
    {
        if(lenArr[i] > lenArr[max])
            max=i;
    }
    int maxLen;
    maxLen=lenArr[max];
    char *temp=calloc(maxLen * sizeof(char));
    va_end(args);

    //sort array arr lexicographically 
    int j,min;
    for(i=0;i<n-1;i++)
    {
        for(min=i,j=i+1;j<n;j++)
           if(strcmp(arr[j],arr[min]) < 0)
                min=j;
           if(min!=i)
           {
              strcpy(temp,arr[i]);
              strcpy(arr[i],arr[j]);
              strcpy(arr[j],temp); 
           }
    }
}

int main(int argc,char **argv)
{
    int n=3;
    sort(n,argv[1],argv[2],argv[3]);//this works when n is pre-defined


    return 0;
} 

How to read n strings as command line arguments in main function and print them sorted?

Also, I am am getting an error in function sort() - redeclaration of args with no linkage.

9
  • Do you mean command line arguments or user input? Commented Sep 20, 2016 at 21:20
  • @ChiefTwoPencils, Command line arguments. Commented Sep 20, 2016 at 21:23
  • You can't do it — well, not without extortionate pain and a special library. Pass the array from the command line to a modified sort function that takes an array. The original code can then use this other function to do the sorting of the array it creates from a variable argument list. The basic problem is that you can't build up a variable number of arguments in a function call argument list at run-time. (But it isn't really clear what your sort function is up to. It sorts some data, but does nothing with the sorted data.) Commented Sep 20, 2016 at 21:26
  • Just pass the argument array in knowing you don't want the first one; easy. Commented Sep 20, 2016 at 21:27
  • 1
    I'm suggesting that it is rather difficult to simulate the effect of sort(3,argv[1],argv[2],argv[3]); and sort(4,argv[1],argv[2],argv[3],argv[4]); without writing the alternatives out like that. OTOH, it is trivial to call sort_alternative(argc-1, argv+1); — as shown in the answer I see at the moment. Commented Sep 20, 2016 at 21:51

1 Answer 1

2
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int cmp(const void *a, const void *b){
    return strcasecmp(*(char**)a, *(char**)b);
}

void sort(int n, char *str[n]){
    qsort(str, n, sizeof(char*), cmp);
}

int main(int argc, char *argv[]){
    sort(argc-1, argv+1);
    for(int i = 1; i < argc; ++i)
        puts(argv[i]);
    return 0;
}

Execution example:

>a.out a B c Z x y
a
B
c
x
y
Z
Sign up to request clarification or add additional context in comments.

3 Comments

Would be better with a bit of explanation
@BLUEPIXY, You didn't use variable number of arguments.
@ufo It is not required if you use the argc and argv as command line arguments. (Also Even if there is a function with a variable argument, it is impossible to pass a variable parameters at runtime.)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.