Skip to content

Instantly share code, notes, and snippets.

@lzzy12
Last active November 9, 2018 19:32
Show Gist options
  • Save lzzy12/e8b2a4b49e36e1d025b4343a551c4b98 to your computer and use it in GitHub Desktop.
Save lzzy12/e8b2a4b49e36e1d025b4343a551c4b98 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include "libs.h"
using namespace std;
int main() {
string password;
bool isValid;
while (true) {
int small = 0, capital = 0, num = 0;
cout << "Enter the password: ";
getline(cin, password);
if (password == "")
break;
if (password.length() >= 6 && password.length() <= 20)
{
for (size_t i = 0; i < password.length(); i++)
{
if (password[i] >= 'A' && password[i] <= 'Z')
{
// Capital letter [A-Z]
capital++;
}
else if (password[i] >= 'a' && password[i] <= 'z') {
// Small number [a-z]
small++;
}
else if (password[i] >= '0' && password[i] <= '9')
{ // Numeral values
num++;
}
if (small > 0 && capital > 0 && num > 0)
{
// Password is found to be valid. no need to continue the loop anymore. This can be an overhead
// but is worth it as it saves much computation when it happens.
break;
}
}
}
if (small > 0 && capital > 0 && num > 0)
{
// Password is valid
cout << "Password is valid" << endl;
}
else
{
// Invalid password
cout << "Password not valid" << endl;
}
}
cin.ignore();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment