Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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]));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading