What’s wrong with my code? What is argv[1]?

I’m trying to ask the user to type in a string so I will print the length of the string. My code is built succeeded. However, when I entered a word and pressed ‘enter’, the program keeps running. I had to enter a second word, then the length of the first string displays. I’m confused at argv[1]. Can someone give me some tips and hint on how to fix this? Thanks in advance for your time.

Please note that I’m not allowed to use any string function.

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

    char* s=argv[1];

    char input[256];
    s = input;
    printf("Please enter a string: ");
    scanf("%s\n", s);
    int str_length = 0;
    while (s[str_length] != '\0')
    {
        str_length++;
        if (s[str_length] == '\0') break;
    }

    printf("%d\n", str_length);


    return 0;   
}

Leave a Comment