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

前端 未结 4 382
梦谈多话
梦谈多话 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:30

    That's because your loop runs over the entire, not just upto the characters you read.

    Instead do,

        size_t len = strlen(arr);
    
        for (i=0; i

    You may also want to use fgets() to avoid buffer overrun. If you enter more than 10 characters, you'll overflow arr, which is undefined behaviour.

    Instead of

    scanf("%s",arr );
    

    do

    fgets(arr, sizeof arr, stdin);
    char *p = strchr(arr, '\n');
    if(p) *p = 0; //trim the trailing newline, if present
    

提交回复
热议问题