Thread: Reorder argv

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    60

    Reorder argv

    I have the following task:
    Write a program that reorders the command-line argument array argv[], placing all strings that start with '-' first, followed by all other strings.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main(int argc, char* argv[])
    {
        char* aux;
        printf("Program name is %s\n", argv[0]);
        if(argc == 1)
            printf("Program called with no arguments.");
        else
        {
            for(int i = 1; i < argc; i++)
            {
                if(argv[i][0] == '-')
                {
                    aux = argv[i];
                    argv[1] = aux;
                    artv[i] = aux;
                }
                printf("Argument %d is: %s", i, argv[i]);
    
    
            }
    
    
        }
        return 0;
    }


    For the command:
    ./a.out string1 -string2 string3 string4
    Program name is ./a.out
    Argument 1 is: string1
    Argument 2 is: string1
    Argument 3 is: string3
    Argument 4 is: string4


    So it basically copied argument 1 into argument 2, but I wanted to swap them. So I should've gotten:
    Argument 1 is: -string2
    Argument 2 is: string1
    Argument 3 is: string3
    Argument 4 is: string4


    Am I getting the first character of each string correctly with argv[i][0]? I google'd this but I didn't find much.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,275
    Your swap is wrong: it should be of the form:
    Code:
    temp = x;
    x = y;
    y = temp;
    The next problem is that you need to swap with a pointer that doesn't have the '-' prefix, not with a fixed pointer. (And you should be searching for pointers without the prefix first.)

    Yes, the first character of argv[i] is argv[i][0]
    Last edited by laserlight; 01-03-2021 at 01:40 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    60
    Thanks!
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main(int argc, char* argv[])
    {
        char* aux;
        printf("Program name is %s\n", argv[0]);
        if(argc == 1)
        printf("Program called with no arguments.");
        else
        {
            for(int i = 1; i < argc; i++)
            {
                for(int j = i+1; j < argc; j++)
                {
                    while(argv[i][0] != '-' && argv[j][0] == '-')
                    {
                        aux = argv[i];
                        argv[i] = argv[j];
                        argv[j] = aux;
                    }
                }
                printf("Argument %d is: %s\n", i, argv[i]);
            }
         }
        return 0;
    }
    Command: ./a.out string1 -string2 string3 -string4 -string5
    Program name is ./a.out
    Argument 1 is: -string2
    Argument 2 is: -string4
    Argument 3 is: -string5
    Argument 4 is: string1
    Argument 5 is: string3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler reorder the statement to excute
    By hqt in forum C++ Programming
    Replies: 4
    Last Post: 02-06-2012, 09:42 AM
  2. argv[]
    By rakeshkool27 in forum C Programming
    Replies: 13
    Last Post: 04-18-2010, 10:36 AM
  3. main() ; argv[1] = string at argv[0]???
    By UCnLA in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 12:16 AM
  4. Question on *argv vs. *argv[]
    By movl0x1 in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 06:03 PM
  5. argv
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 06:41 PM

Tags for this Thread