Home > Article > Backend Development > C++ program to check if a given password is strong
Suppose we have a string S. S is a password. If the password is complex and meets all of the following conditions -
The password must be at least 5 characters long;
The password must contain at least one uppercase letter;
The password must contain at least one lowercase letter;
The password must contain at least one digit.
We must check the quality of password S.
To solve this problem, we need to operate on strings. Strings in programming languages are A stream of characters stored in a specific array-like data type. multilingual Specify strings as specific data types (e.g. Java, C, Python); and several other languages Specify the string as a character array (such as C). Strings are useful in programming because they Often the preferred data type in a variety of applications and used as an input data type and output. There are various string operations such as string search, substring generation, String stripping operation, string translation operation, string replacement operation, string Reverse operation and so on. Check out the link below to see how strings Used in C/C.
https://www.tutorialspoint.com/cplusplus/cpp_strings.htm
https://www.tutorialspoint.com/cprogramming/c_strings. htm
So if the input to our problem is something like S = "NicePass52", the output will be Strong.
To solve this problem we will follow the following steps-
a := false, b := false, c := false, d := false if size of s >= 5, then: a := true for initialize i := 0, when i < call length() of s, update (increase i by 1), do: if s[i] >= '0' and s[i] <= '9', then: b := true if s[i] >= 'A' and s[i] <= 'Z', then: c := true if s[i] >= 'a' and s[i] <= 'z', then: d := true if a, b, c and d all are true, then: return "Strong" Otherwise return "Weak"
Let us see the following implementation for better understanding -
#include <bits/stdc++.h> using namespace std; string solve(string s){ bool a = false, b = false, c = false, d = false; if (s.length() >= 5) a = true; for (int i = 0; i < s.length(); i++){ if (s[i] >= '0' && s[i] <= '9') b = true; if (s[i] >= 'A' && s[i] <= 'Z') c = true; if (s[i] >= 'a' && s[i] <= 'z') d = true; } if (a && b && c && d) return "Strong"; else return "Weak"; } int main(){ string S = "NicePass52"; cout << solve(S) << endl; }
"NicePass52"
Strong
The above is the detailed content of C++ program to check if a given password is strong. For more information, please follow other related articles on the PHP Chinese website!