Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:07:33+00:00 2026-06-11T16:07:33+00:00

I need to make a program that accepts no less than 2 and no

  • 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:07:34+00:00Added an answer on June 11, 2026 at 4:07 pm

    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] );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make a program that adds long integers without using the biginteger
I need to be able to make a program that looks through a mailbox
I need to make the program which have one form that contains PNG image
For my project, I need to make a program that takes 10 numbers as
I'm trying to write a program that modifies fractions and I need to make
I have a program (in C) that runs in the command line, and I
I need to make a program that runs a process (my another programm) and
I'm on a Linux environment and I need to make a program that retrieves
I need to make a program that reads a file, and puts all of
I need to make an online image resizing program that uses the Lanczo's Algorithm,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.