-2

here is a part of my code:

int main(int argc, char*argv[])  
{
 //This section shows the statistic of the program
int a;

printf("The number of arguments is :%d \n", argc);

for(a=0; a<argc; a++)
{
        printf("argc %d is %s \n",a, argv[a]);
}

Based on the code above, the second command line argument argv[1] will be a .txt file.

The third command line argument argv[2] will be entered as a string such as "monkey".

I need to know how to store the second command line argument "monkey" as an array of individual character so it will look somethings like this:

char key[6];
     key[0]= 'm';
     key[1]= 'o';
     key[2]= 'n';
     key[3]= 'k';
     key[4]= 'e';
     key[5]= 'y';

1 Answers1

-1

Try this:

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


int main(int argc, char*argv[])
{
        //This section shows the statistic of the program
        int a;

        printf("The number of arguments is :%d \n", argc);

        for(a=0; a<argc; a++)
        {
                printf("argc %d is %s \n",a, argv[a]);
        }

        size_t len = strlen(argv[2]);

        int key[10];
        int i = 0;
        for(i = 0; i < len; i++)
        {
                key[i] = argv[2][i];
        }

        for (i = 0; i < len; i++)
        {
                printf("key[%d] = %c\n", i, key[i]);
        }
}
msc
  • 33,420
  • 29
  • 119
  • 214