Answer to Question #291513 in C++ for Abdullah

Question #291513

Imagine you are developing a software system that requires users to enter their own passwords.

Your software requires that user’s password meet the following criteria:

 The password should be at least eight characters long.

 The password should contain at least one uppercase and at least one lowercase letter.

 The password should have at least one special character.

Write a program that asks for a password and then verifies that it meets the stated criteria. If it

doesn’t, the program should display a message telling the user why.


1
Expert's answer
2022-01-28T07:59:36-0500
#include <iostream>
#include <string>
#include <algorithm>


bool has_special_char(std::string const& str) {
    return std::find_if(str.begin(), str.end(),
        [](unsigned char ch) { return !(isalnum(ch) || ch == '_'); }) != str.end();
}


int main()
{
    std::string password;
    std::cout << "Enter password: "<< std::endl;
    while (true)
    {
        std::getline(std::cin, password);


        if (password.size() < 8)
        {
            std::cout << "Your passworrd should be at least 8 symbols" << std::endl;
            continue;
        }
         
        if (!(std::any_of(password.begin(), password.end(), islower))) 
        { 
            std::cout << "Your password doesn't contain lowercase letter" << std::endl;
            continue;
        }


        if (!(std::any_of(password.begin(), password.end(), isupper)))
        {
            std::cout << "Your password doesn't contain uppercase letter" << std::endl;
            continue;
        }


        if (!has_special_char(password))
        {
            std::cout << "Your password doesn't contain special character" << std::endl;
            continue;
        }


        std::cout << "Your password is good";
        break;


    }
  
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog