c - I have an issue printing the length of a string from the command line without using strlen? -
hello there program meant when user enters string command line print word , size.
e.g
./commandline hello world
output:
hello
world
2
what i'm trying add method print length without using strlen
output should length 10 example above.
this code bare in mind new c.
int main(int args, char *argv[]){ for(int =1; <args; i++){ printf("%s\n", argv[i]); size_t(argv[i]); } printf("%d\n", args -1); } size_t string_length( char *argv[]){ int length = 0; while(argv[length]!='\0') { length++; printf("%i\n", length); } return 0; }
my program not print length prints string entered , size.
for(int =1; <args; i++){ printf("%s\n", argv[i]); size_t(argv[i]); } printf("%d\n", args -1);
here you're not calling function anywhere. program prints arguments , number of them. size_t(argv[i]);
merely casts argv[i]
type called size_t
. certainly, that's not want. replace string_length(argv[i]);
. note you'd better change type of first argument of function.
what's more, should return length
in string_length
function.
size_t string_length( char *arg){ size_t length = 0; while(arg[length]) { length++; } return length; }
Comments
Post a Comment