• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 646
  • Last Modified:

Argc and Argv in C

I've written this code, but I'm having problems executing it.  The assignment is to use argsort function to put words in alphabetic order.  What am I missing???
#include <stdio.h>
#include <string.h>
 
   void main( int argc, char *argv[] )
   {
	   int i;
	   char word;
	   int len = 0;
 
    if (len < 3)
    {
		printf("Enter a word:\n");
		scanf("%c\n", word);
//	            return 1;
	}
	 
	for( i=0; i<argc; i++ ){
       printf("argv[%d] = %s\n",i,argv[i]);
	}
//   return 1;
   }

Open in new window

0
clscor2
Asked:
clscor2
  • 2
  • 2
1 Solution
 
Infinity08Commented:
A few things. See my comments in the code below. Note that I also properly indented the code so it's easier to read.
#include <stdio.h>
/*#include <string.h>*/                           /* <--- not used, so you can leave it out */
 
int main(int argc, char *argv[]) {                /* <--- main HAS to return int, not void */
    int i = 0;                                    /* <--- always initialize your variables */
    char word[128] = "";                          /* <--- a word will probably be more than 1 character,
                                                          so make it an array of characters (better
                                                          known as a string) instead */
    int len = 0;
 
    if (len < 3) {
        printf("Enter a word:\n");
        scanf("%s", word);                        /* <--- %s for a string, %c for a character -
                                                          leave out the \n - you don't need it */
    }
         
    for (i = 0; i < argc; i++) {
        printf("argv[%d] = %s\n", i, argv[i]);
    }
 
    return 0;                                     /* <--- usually, in case of success, 0 is returned */
}

Open in new window

0
 
ozoCommented:
where is the argsort function?
0
 
clscor2Author Commented:
Thank you!  Your help was much appreciated.  I do have another question.  If I needed to prompt for several strings (rather than just 1), then alphabetize them, what am I missing?
0
 
Infinity08Commented:
>> If I needed to prompt for several strings (rather than just 1),

Use a loop, instead of a single if.

>> then alphabetize them, what am I missing?

Store each of the strings you read into an array, and then sort the array.
0
 
clscor2Author Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now