0

I need to make a program that accepts no less than 2 and no more than 6 arguments at the command line and then prints out the 1st or 2nd character EX: asdf asdf asdf asdf prints out as: a s a s

I have the initial array setup and working, the for loops below are meant to cut the string off at a space in the input and copy it to a new string, but it is not working correctly. I am new to C, and to this site. Any help is greatly appreciated.

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

int main(){

char a[50];
char b[50];
char c[50];
char d[50];
char e[50];
char f[50];

int i;

printf("enter a string (Ex: asdf asdf asdf... Must have atleast 2 arguments but no more than six): ");
scanf("%s", a);
printf("%c", a);

for (i = 0; i != 50; i++){

      if(a[i]==' '){
      char strncpy(b, &a[i], i+2);
      printf("\n%c ",a[1]);
      printf("%c ",b[0]);
      }
}
for (i = 0; i != 50; i++){    
      if(b[i]==' '){
      char strncpy(c, &b[i], i+2);
      printf("%c ",c[1]);
      }
}
for (i = 0; i != 50; i++){    
      if(c[i]==' '){
      char strncpy(d, &c[i], i+2);
      printf("%c ",d[0]);
      }
}
for (i = 0; i != 50; i++){    
      if(d[i]==' '){
      char strncpy(e, &d[i], i+2);
      printf("%c ",e[1]);
      }
}
for (i = 0; i != 50; i++){    
      if(e[i]==' '){
      char strncpy(f, &e[i], i+2);
      printf("%c ",f[0]);
      }
}
return 0;
}
Church
  • 115
  • 2
  • 3
  • 11
  • Am I missing something? When you say 'passing arguments at the command line' I would expect that you're talking about the `argv` array passed into `main()`. Is that not the case? I have prepared an answer assuming it is, but perhaps you could clarify first. – paddy Sep 18 '12 at 23:23
  • Yes, that is what i'm suppose to do. I however just tried doing it anyway possible after awhile – Church Sep 18 '12 at 23:27
  • Hmm, I see that you've edited your question so that you are reading from the console, not the command-line. I posted my command-line answer after you said that's what you're actually trying to do. I will remove it if that isn't what you meant. – paddy Sep 18 '12 at 23:42
  • I'm not actually entirely sure what the teacher wants, the book also says command line on every problem, but the teacher has accepted programs not using the command line. Your answer is great, I appreciate the help! – Church Sep 18 '12 at 23:51

1 Answers1

0

You don't need to copy your strings out of anywhere... Coming from the command line you'll have them sitting in argv:

int main( int argc, char **argv )
{

}

Where argc is the total number of arguments plus 1 (the first is the name that invoked your program), and argv is an array of pointers to each argument string. These have already been tokenised from the command-line.

So first you wanna test you have enough arguments. I like to explicitly make a new variable to remove the off-by-one confusion from comparisons:

int nparams = argc - 1;
if( nparams < 2 || nparams > 6 ) {
    printf( "Wrong number of arguments\n" );
    return -1;
}

Then you loop over your arguments. The first will be in position 1 of the array... From your example, it seems that you print the first character of the first argument, and the second character of the next, then continue alternating. That's a modulo operation. I have a variable which that chooses which character to print.

int i, which;
for( i = 1; i <= nparams; i++ ) {
    which = (i-1) % 2;
    printf( "%c\n", argv[i][which] );
}

This does assume that every second argument is at least two characters long. No error checking. If you need error checking, you need to make sure that the character you're printing is not the string-terminator (value 0). In the case of the second character, you also need to check the value before it is not 0. I don't know if it's possible to specify an argument that is a string of zero length. Perhaps a reader who does know can comment.

Well, I may as well put it in... So your loop would look a little like this:

if( argv[i][0] == 0 || argv[i][which] == 0 ) {
    printf( "\n" );
} else {
    printf( "%c\n", argv[i][which] );
}
paddy
  • 60,864
  • 6
  • 61
  • 103