0

I was told to remake this with proper comments and the actually code I'm working on. I'm attempting to take a command line input and put it into an array. I want to then parse this array into two new arrays.

The problems are argv[1] is a char * so I'm running into problems converting into an INT. I also can't figure out how to take each element from argv[1], (e.g. 1010101) and place those 1's and 0's into an array. Once I figure this out, I'm going to take this array and parse it if the input is greater than 5. The command line argument coming in is going to be either 5 length or 10 length. If its 5 I do nothing, if its 10 I parse the input into two arrays.

#include <stdio.h>
#include <stdlib.h>


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


int i;
//starting using char * arrays in order to try to grab input from the char * argv[1] command line arg
char *aA[10] = {0};
char *aB[10] = {0};
char *aC[10] = {0}; 
char *s = '1';
char *k = '0';

//read in from command the command line.  Print the arguments and save
//the 1 and 0 inputs into an array.
//need to check for EOF and next lines
for (i = 0; i < argc; i++)
{
    if (argv[i] == (s | k)) //attempting to find a way to look at 1 and 0 as a char
    {
        aA[i] = argv[i]; //place the 1 or 0 into array aA
    }

    printf("arg %d: %s\n", i,  argv[i]);
}

printf("\n");

//print array aA to see if it caught all of the 1's and 0's from the command line argument
for (i = 0; i < 8; i++)
{
    printf("%s ", aA[i]);
}

//next check if array aA is 5 strlen or 10 strlen
//if 5, do nothing
//if 10, parse array aA into two arrays aB and aC
//aB gets a[0] a[2] a[4] a[6] a[8]
//aC gets a[1] a[3] a[5] a[7] a[9] 
//print the results of aB and aC to make sure aA was correctly parsed   


printf("\n");
return 0;

}
5
  • 1
    Please provide an example of what you mean: what command line arguments are you expecting, and what do you want to do with them? Commented Dec 2, 2014 at 19:05
  • 10101 a 1000110001 Above is a sample input from the command line. I need to take the 10101 and put that into an array, check the next line and notice that its an a which tells me to preform an addition. Next I check the last line which is a combination of two numbers, which will need to be parsed into two arrays before I can perform any actions on them Commented Dec 2, 2014 at 19:11
  • should I attempt to use atoi? like make an int b = atoi argv[1] and then I would have a int # like 10001, maybe theres a way to break this into single numbers by parsing with maybe a mask like aA[0] = b | 0x10000 to grab the first 1? Commented Dec 2, 2014 at 19:20
  • What logic do you use to conclude that 1000110001 is a combination of two numbers? Commented Dec 2, 2014 at 19:38
  • each number is 5 digits long, so a ten digit length string will be two numbers Commented Dec 2, 2014 at 21:07

1 Answer 1

1
given a command line: myexecutable 10101   or myexecutable 1010101010

char originalArray[10] = {'\0'};
int array1[5] = {0};
int array2[5] = {0};
int i; // loop counter

if (2 == argc)
{ // then parameter exists
    if( (10 == strlen(argv[1])) || (5 == strlen(argv[1]) )
    { // then valid parameter length
        strncpy( originalArray, argv[1], strlen(argv[1]) );
    }

    else
    { // not valid parameter length
        // handle error
        exit( EXIT_FAILURE );
    }


    for( i=0; i<5; i++ )
    {
        if( ('1' == originalArray[i]) || ('0' == originalArray[i]) )
        { // then valid char 
            array1[i] = originalArray[i] - '0';
        }

        else
        { // invalid char in parameter
            // handle error
             exit( EXIT_FAILURE );
        } // end if
    } // end for


    if( 10 == strlen(originalArray) )
    { // then set second array
        for( i=5; i<10;i++ )
        {
            if( ('1' == originalArray[i]) || ('0' == originalArray[i]) )
            { // then valid character
                array2[i-5] = originalArray[i] - '0';
            }

            else
            { // invalid char in parameter
                // handle error
                exit( EXIT_FAILURE );
            } // end if
        } // end for
    } // end if
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.