C++ Program to check whether given password is strong or not


Suppose we have a string S. S is a password. If a password is complex, if it meets all of the following conditions −

  • Password length is at least 5 characters;

  • Password contains at least one uppercase letter;

  • Password contains at least one lowercase letter;

  • Password contains at least one digit.

We have to check the quality of the password S.

Problem Category

To solve this problem, we need to manipulate strings. Strings in a programming language are a stream of characters that are stored in a particular array-like data type. Several languages specify strings as a specific data type (eg. Java, C++, Python); and several other languages specify strings as a character array (eg. C). Strings are instrumental in programming as they often are the preferred data type in various applications and are used as the datatype for input and output. There are various string operations, such as string searching, substring generation, string stripping operations, string translation operations, string replacement operations, string reverse operations, and much more. Check out the links below to understand how strings can be used in C/C++.

https://www.tutorialspoint.com/cplusplus/cpp_strings.htm

https://www.tutorialspoint.com/cprogramming/c_strings.htm

So, if the input of our problem is like S = "NicePass52", then the output will be Strong.

Steps

To solve this, we will follow these 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"

Example

Let us see the following implementation to get 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;
}

Input

"NicePass52"

Output

Strong

Updated on: 07-Apr-2022

826 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements