1

This is a part of my code (a boolean function) ,so the goal is to return true or false. It's working pretty well, but I was wandering if there were a better way. Instead of using those integers, is there a shorter way? thanks....

int l = strlen(password);
int b, c, d, e;

for (int i = 0; i < l; i++)
{
    if (isupper(password[i]))
    {
        b = 1;
    }
    else if (islower(password[i]))
    {
        c = 1;
    }
    else if (isdigit(password[i]))
    {
        d = 1;
    }
    else if (isgraph(password[i]))
    {
        e = 1;
}

int a = b + c + d + e;
if (a == 4)
{
    return true;
}   
else
{
    return false;
}
2
  • hey, i'm currently on this question as well and though i did the same thing as you, i wanted to ask if check50 gives you an issue wrt the periods/full stops - i.e. the ":( Password of VnrHMtV4 is rejected for lack of symbols." error?
    – blueetfm
    Commented Mar 26, 2023 at 3:19
  • I'm really sorry for the late response I missed the mailed, no when I used this code it gave me no errors as far as I remember, I can send you the rest of the code if you want.
    – Ziad Ayman
    Commented Jul 22, 2023 at 4:12

1 Answer 1

0

I was having a lot of trouble trying to do my boolean using switch-case and your code helped me to finish mine. Although I did it a little different and I think that "more correct", too.Instead of storing the values in int variables, I used boolean ones. In that way, everytime the 4 expressions are proven, the password will be accepted. A problem I see in your code is that if the user types 4 lower letters for example, the program will consider it correct because a lower letter is accepted and was used 4 times, in a way that 'a' now is 4 indeed. But that's just my speculation. Hope this comment can be useful for you and others. And sorry if I made any mistakes, not a native of the language!

bool valid(string password)

{

bool a, b, c, d;

for(int i = 0; i < strlen(password); i++)
{
    if(isupper (password[i]))
    {
        a = true;
    }

    else if (islower(password[i]))
    {
        b = true;
    }

    else if (ispunct(password[i]))
    {
        c = true;
    }

    else if (isdigit(password[i]))
    {
        d = true;
    }
}

if (a && b && c && d == true)
{
    return true;
}

else
{
    return false;
}

}

1
  • Actually no the code won't consider it correct for the 4 low cases because it sets the value of a certain variable to 1 for each condition, it doesn't increase it by one .... therefore if there were a thousand lowcases the value of c would always be 1 so it's gonna be correct, but still your code is better than mine, they both perform the same but yours uses the correct types of variable which is better
    – Ziad Ayman
    Commented Jul 22, 2023 at 4:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .