Home  >  Article  >  Backend Development  >  C++ program to check if a given password is strong

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

王林
王林forward
2023-09-29 23:05:061125browse

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] >= &#39;0&#39; and s[i] <= &#39;9&#39;, then:
      b := true
   if s[i] >= &#39;A&#39; and s[i] <= &#39;Z&#39;, then:
      c := true
   if s[i] >= &#39;a&#39; and s[i] <= &#39;z&#39;, then:
      d := true
if a, b, c and d all are true, then:
   return "Strong"
Otherwise return "Weak"

Example

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] >= &#39;0&#39; && s[i] <= &#39;9&#39;)
         b = true;
      if (s[i] >= &#39;A&#39; && s[i] <= &#39;Z&#39;)
         c = true;
      if (s[i] >= &#39;a&#39; && s[i] <= &#39;z&#39;)
         d = true;
   }
   if (a && b && c && d)
      return "Strong";
   else
      return "Weak";
}
int main(){
   string S = "NicePass52";
   cout << solve(S) << endl;
}

Input

"NicePass52"

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete