Thread: Command-line argument echoing in reverse

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    52

    Command-line argument echoing in reverse

    I am trying to echo the command line arguments. An example would be if you the user entered "a.out null and void" then it would echo back "void and null."

    I have a program written to echo back exactly what the user enters but I can't think of a way to make it do it in reverse order. Here's the code I have:

    Code:
    int main(int argc, char *argv[])
       {
           int i;
    
           for (i = 1; i < argc; i++)
               printf("%s%s", argv[i], (i < argc-1) ? " " : "");
           printf("\n");
           return 0;
       }

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        while (argc-- > 0)
            printf("%s ", argv[argc]);
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    k, thanks for the help.

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You could also just reverse your for loop (initialise i to argc-1, count down to 1, subtract 1 from i each iteration).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my reverse function and its output!
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 08:33 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  5. Replies: 7
    Last Post: 03-18-2003, 03:32 PM