-1

I am wondering how I can printf a specific part of the argv. For example

./program hello world
The first 3 letters of the first argument is:
1 = h
2 = e
3 = o
The full argument was: hello

The first 3 letters of the second argument is:
1 = w
2 = o
3 = r
The full argument was: world

Here is my code, I just don't know what to put in that specific place

int main(int argc, char **argv) {
    printf("The first 3 letters of the first argument is:\n");
    int i = 0;
    while (i < 3) {
        printf("%d = %c\n", .... I don't know what to put here);
        i++;
    }
    printf("The full word was: %s\n\n", I don't know what to put here);

    printf("The first 3 letters of the second argument is:\n");
    int j = 0;
    while (j < 3) {
        printf("%d = %c\n", j, .... I don't know what to put here);
        j++;
    }
    printf("The full word was: %s\n", I don't know what to put here);
}
James Coles
  • 51
  • 1
  • 5
  • The arguments are stored in `argv`. This other question might help: [C - reading command line parameters](https://stackoverflow.com/questions/5157337/c-reading-command-line-parameters) – that other guy Aug 10 '18 at 01:07
  • 2
    Why do you print `h` `e` `o` instead of `h` `e` `l`? – Barmar Aug 10 '18 at 01:16

3 Answers3

1

The first argument is in argv[1], the second argument is argv[2], and so on. To print individual characters from them, add another level of subscripting.

You should also check that the arguments were provided before printing them, and that they have enough characters to print in the loops.

int main(int argc, char **argv) {
    if (argc >= 2) {
        printf("The first 3 letters of the first argument is:\n");
        int i = 0;
        while (i < 3 && argv[1][i]) {
            printf("%d = %c\n", i, argv[1][i]);
            i++;
        }
        printf("The full word was: %s\n\n", argv[1]);
    }

    if (argc >= 3) {
        printf("The first 3 letters of the second argument is:\n");
        int j = 0;
        while (j < 3 && argv[2][j]) {
            printf("%d = %c\n", j, argv[2][j]);
            j++;
        }
        printf("The full word was: %s\n", argv[2]);
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

The first element in argv is the name of the program, and the remaining elements point to arguments. The first three characters of each argument can be found by iterating through the arguments argv[1] through argv[n], while printing the desired characters for each argument.

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

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

    printf("Program:    %s\n", argv[0]);

    if (argc > 2)
    {
        int i = 1;
        for ( ; i < argc ; i++)
        {
            if (!argv[i][2])
            {
                fprintf(stderr,"Length of argument \"%s\" is less than 3\n", argv[i]);
                return(EXIT_FAILURE);
            }
            else
                printf("The first 3 letters of argument %d is:\n", i);
                int j = 0;
                while(j < 3)
                {
                    printf("Index %d in agument \"%s\" is %c\n", j, argv[i], argv[i][j]);
                    j++;
                }
        }
    }
    else
        fprintf(stderr,"Missing arguments\n");
        return(EXIT_FAILURE);

    return(EXIT_SUCCESS);
}
TruBlu
  • 441
  • 1
  • 4
  • 15
0

Code:

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

const int num_chars = 3;
int main(int argc, char **argv) {
    if(argc == 1) return 1;
    if(num_chars <= 0) return 1;

    for(int i=1; i<argc; i++)
    {
       if(num_chars > strlen(argv[i])) return 1;
       printf("The first %d chars of %s is:\n", num_chars, argv[i]);
       for(int j=0; j<num_chars; j++)
       {
         printf("%d = %c\n", j, argv[i][j]);
       }
       printf("\n");
    }
    return 0;
}

Valid case:

$ ./test hello world
The first 3 chars of hello is:
0 = h
1 = e
2 = l

The first 3 chars of world is:
0 = w
1 = o
2 = r

Invalid case:

$ ./test hel wo
The first 3 chars of hel is:
0 = h
1 = e
2 = l
Andy J
  • 1,479
  • 6
  • 23
  • 40