How to count the number of characters in a command line argument by using and creating a function

Xavier Dass

I am having trouble understanding how to create a function to count the characters in a command line argument. It only has to compute the result in the 'my_strlen()' function, but to print the result out in main(). I am very new to C, but here is my code so far;

int my_strlen(  char string[]);
{
     strcpy(string, argv[1];
     return 1;
}
int main(int argc, char *argv[])
{
  if(argv != 2)
  {
    printf("You must run this program with an argument\n");
    return 2;
  }
  printf("%d", strlen(string);
  return 0;
}

So as you can see, I am pretty confused, I also didn't know how to store the value of strlen(string) to call it later as its own integer.

ani627

Try this simple code.

#include <stdio.h>
#include <string.h>
int my_strlen(char *string) //Function to calculate length of given string
{
    int i;
    for(i=0;string[i]!='\0';i++);
    return i;
}
int main(int argc, char *argv[])
{
  int length;
  if(argc != 2) //Check the number of command line arguments
  {
    printf("You must run this program with an argument\n");
    return 2;
  }
  else
  {
    length = my_strlen(argv[1]); //Your function to calculate length of string
    printf("Length of command line argument is: %d\n", length);
  }
  return 0;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to count the number of characters on each line in vim?

How to count the number of characters on each line with group by

How to count the number of characters in a line in a csv file

How to pass a command line argument into a kdb function

Printing command line argument tokens using strtok function. How to return a pointer to pointer using this function

How can I count the number of specific files within a date range on the command line using bash?

How to count the number of characters in a line, except a specific character?

How to pass negative number as a command line argument in Erlang

How do you add a number as a command line argument?

Command line argument not working when passing to a function while using getopts

How to copy a number of files using command line?

Creating a Command Line Argument System for Embedded Python

How to pass array as an argument to a Ruby function from command line?

How to count number of occurence in a function using python

How to use sed or another command line function to remove . between characters

How to pass json string as a command line argument using python

How do I take a vector input using command line argument?

How to pass string as command line argument in python using C#

how to Pass command line argument ( baseURL) using java for selenium WebDriver

AWK - How to filter using a command line argument ($1)

How to count number characters of strings without using strlen

How to uppercase the command line argument?

How to count the characters of a hexadecimal number

How to remove characters from file names using command line?

How to calculate number of characters per line using monospace font

Using break command as argument to function

How can I get a count of files in a directory using the command line?

How to count items in JSON object using command line?

How can I pass an IP address used in a function as command line argument to argument parser?