Input a password from the user and check if it has a char, alphabet and digit

前端 未结 4 387
梦谈多话
梦谈多话 2021-01-23 19:48

My doubt is that, the program works fine if I enter a password that fills the array. However if I enter a password such as \"apple25\", I think it counts the blank spaces in the

4条回答
  •  天涯浪人
    2021-01-23 20:25

    I think it counts the blank spaces in the array as charecters and even declares "apple25" as a valid password.

    Your assumption is almost right. Your program does count characters after the end of user input, but they are not necessarily blanks.

    You get this effect with "AAPPLLEE2" because your code examines ten characters of a nine-character string. The tenth character is null terminator, which is neither a digit nor a letter.

    You need to use the actual length for the limit of the loop, rather than hard-coding the limit of ten.

    You should also fix the potential buffer overrun:

    char arr[11];       // <<== Add one more character for '\0'
    printf("Enter a password. \n");
    scanf("%10s",arr ); // <<== Tell scanf how big is your buffer
    

提交回复
热议问题