Home > OS >  How to find the length of a command line arguments individual strings
How to find the length of a command line arguments individual strings

Time:07-09

Say I run the following in my command line

$./(file name) abcd efg hijkl

How would I best find the length of argv[1],argv[2], etc.

In this example, I would like to hold argv[1], or "abcd", as an integer with a value of 4, argv[2], or "efg", as an integer value of 3, and argv[3], or "hijkl", as an integer value of 5.

EDIT: realized I forgot to write #include <string.h> ... smh

CodePudding user response:

strlen is used to find the length of strings.

#include <string.h>
#include <stdio.h>
 
int main(int argc, char *argv[])
{
    int i;

    for (i = 0; i < argc; i  )
        printf("%zu\n", strlen(argv[i]));
}
  • Related