Home > Backend Development > C++ > body text

C++ program to check if a given password is strong

王林
Release: 2023-09-29 23:05:06
forward
950 people have browsed it

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.

Problem Category

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.

Steps

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"
Copy after login

Example

Let us see the following implementation for better understanding -

#include 
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;
}
Copy after login

Input

"NicePass52"
Copy after login

Output

Strong
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!