Strong Password Suggester Program

26 Jul, 2021
Given a password entered by the user, check its strength and suggest some password if it is not strong.
Criteria for strong password is as follows : A password is strong if it has : 1. At least 8 characters 2. At least one special char 3. At least one number 4. At least one upper and one lower case char. 
Examples : 
Input : keshav123
Output : Suggested Password
k(eshav12G3
keshav1@A23
kesh!Aav123
ke(shav12V3
keGshav1$23
kesXhav@123
keAshav12$3
kesKhav@123
kes$hav12N3
$kesRhav123

Input :rakesh@1995kumar
Output : Your Password is Strong
Approach : Check the input password for its strength if it fulfills all the criteria, then it is strong password else check for the absent characters in it, and return the randomly generated password to the user.  
C++
Java
Python3
C#
PHP
// CPP code to suggest strong password#include <bits/stdc++.h> using namespace std; // adding more characters to suggest strong passwordstring add_more_char(string str, int need){    int pos = 0;     // all 26 letters    string low_case = "abcdefghijklmnopqrstuvwxyz";     for (int i = 0; i < need; i++) {        pos = rand() % str.length();        str.insert(pos, 1, low_case[rand() % 26]);    }    return str;} // make powerful stringstring suggester(int l, int u, int d, int s, string str){     // all digits    string num = "0123456789";     // all lower case, uppercase and special characters    string low_case = "abcdefghijklmnopqrstuvwxyz";    string up_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";    string spl_char = "@#$_()!";     // position at which place a character    int pos = 0;     // if there is no lowercase char in input string, add it    if (l == 0) {        // generate random integer under string length        pos = rand() % str.length();         // generate random integer under 26 for indexing of a to z        str.insert(pos, 1, low_case[rand() % 26]);    }     // if there is no upper case char in input string, add it    if (u == 0) {        pos = rand() % str.length();        str.insert(pos, 1, up_case[rand() % 26]);    }     // if there is no digit in input string, add it    if (d == 0) {        pos = rand() % str.length();        str.insert(pos, 1, num[rand() % 10]);    }     // if there is no special character in input string, add it    if (s == 0) {        pos = rand() % str.length();        str.insert(pos, 1, spl_char[rand() % 7]);    }     return str;} /* make_password function :This function is used to checkstrongness and if input string is not strong, it will suggest*/void generate_password(int n, string p){    // flag for lower case, upper case, special    // characters and need of more characters    int l = 0, u = 0, d = 0, s = 0, need = 0;     // password suggestions.    string suggest;     for (int i = 0; i < n; i++) {        // password suggestions.        if (p[i] >= 97 && p[i] <= 122)            l = 1;        else if (p[i] >= 65 && p[i] <= 90)            u = 1;        else if (p[i] >= 48 && p[i] <= 57)            d = 1;        else            s = 1;    }     // Check if input string is strong that    // means all flag are active.    if ((l + u + d + s) == 4) {        cout << "Your Password is Strong" << endl;        return;    }    else        cout << "Suggested password " << endl;     /*suggest 10 strong strings */    for (int i = 0; i < 10; i++) {        suggest = suggester(l, u, d, s, p);        need = 8 - suggest.length();        if (need > 0)            suggest = add_more_char(suggest, need);        cout << suggest << endl;    }} // Driver codeint main(){    string input_string = "geek@2018";    srand(time(NULL));     // srand function set Seed for rand().    generate_password(input_string.length(), input_string);    return 0;}
// Java code to suggest strong passwordimport java.io.*;import java.util.*;import java.util.Random; public class GFG {     // adding more characters to suggest    // strong password    static StringBuilder add_more_char(                        StringBuilder str, int need)    {        int pos = 0;        Random randm = new Random();                 // all 26 letters        String low_case = "abcdefghijklmnopqrstuvwxyz";         for (int i = 0; i < need; i++) {            pos = randm.nextInt(1000) % str.length();            str.setCharAt(pos,low_case.charAt(                            randm.nextInt(1000) % 26));        }        return str;    }     // make powerful String    static StringBuilder suggester(int l, int u, int d,                              int s, StringBuilder str)    {        Random randm = new Random();                 // all digits        String num = "0123456789";         // all lower case, uppercase and special        // characters        String low_case = "abcdefghijklmnopqrstuvwxyz";        String up_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";        String spl_char = "@#$_()!";         // position at which place a character        int pos = 0;         // if there is no lowercase char in input        // String, add it        if (l == 0) {                         // generate random integer under            // String length()            pos = randm.nextInt(1000) % str.length();             // generate random integer under 26 for            // indexing of a to z            str.setCharAt(pos,low_case.charAt(randm.nextInt(1000)                                        % 26));        }         // if there is no upper case char in input        // String, add it        if (u == 0) {            pos = randm.nextInt(1000) % str.length();            str.setCharAt(pos,low_case.charAt(randm.nextInt(1000)                                        % 26));        }         // if there is no digit in input String, add it        if (d == 0) {            pos = randm.nextInt(1000) % str.length();            str.setCharAt(pos,low_case.charAt(randm.nextInt(1000)                                        % 10));        }         // if there is no special character in input        // String, add it        if (s == 0) {            pos = randm.nextInt(1000) % str.length();            str.setCharAt(pos,low_case.charAt(randm.nextInt(1000)                                        % 7));        }         return str;    }     /* make_password function :This function is used    to check strongness and if input String is not    strong, it will suggest*/    static void generate_password(int n, StringBuilder p)    {                 // flag for lower case, upper case, special        // characters and need of more characters        int l = 0, u = 0, d = 0, s = 0, need = 0;         // password suggestions.        StringBuilder suggest;         for (int i = 0; i < n; i++) {                         // password suggestions.            if (p.charAt(i) >= 97 && p.charAt(i) <= 122)                l = 1;            else if (p.charAt(i) >= 65 && p.charAt(i) <= 90)                u = 1;            else if (p.charAt(i) >= 48 && p.charAt(i) <= 57)                d = 1;            else                s = 1;        }         // Check if input String is strong that        // means all flag are active.        if ((l + u + d + s) == 4) {            System.out.println("Your Password is Strong");            return;        }        else            System.out.println("Suggested password ");         /*suggest 10 strong Strings */        for (int i = 0; i < 10; i++) {            suggest = suggester(l, u, d, s, p);            need = 8 - suggest.length();            if (need > 0)                suggest = add_more_char(suggest, need);            System.out.println(suggest);;        }    }     // Driver code    public static void main(String[] args)    {        StringBuilder input_String = new StringBuilder("geek@2018");        generate_password(input_String.length(), input_String);    }} // This code is contributed by Manish Shaw (manishshaw1)
# Python code to suggest strong password import random # Function to insert character in a string# Because string is immutable in pythondef insert(s, pos, ch):        return(s[:pos] + ch + s[pos:])         # adding more characters to# suggest strong passworddef add_more_char(s, need):        pos = 0         # add 26 letters        low_case = "abcdefghijklmnopqrstuvwxyz"         for i in range(need):                pos = random.randint(0, len(s)-1)                s = insert(s, pos, low_case[random.randint(0,25)])         return(s) # Make powerful stringdef suggester(l, u, d, s, st):         # all digits        num = '0123456789'         # all lower case, upper case and special characters        low_case = "abcdefghijklmnopqrstuvwxyz"        up_case = low_case.upper()        spl_char = '@#$_()!'         # Position at which place a character        pos = 0         # If there is no lowercase char        # in input string, add it        if( l == 0 ):                           # Generate random integer under string length                pos = random.randint(0,len(st)-1)                 # Generate random integer under 26 for indexing of a to z                st = insert(st, pos, low_case[random.randint(0,25)])         # If there is no upper case char in input string, add it        if( u == 0 ):                           # Generate random integer under string length                pos = random.randint(0,len(st)-1)                 # Generate random integer under 26 for indexing of A to Z                st = insert(st, pos, up_case[random.randint(0,25)])         # If there is no digit in input string, add it        if( d == 0 ):                           # Generate random integer under string length                pos = random.randint(0,len(st)-1)                 # Generate random integer under 10 for indexing of 0 to 9                st = insert(st, pos, num[random.randint(0,9)])         # If there is no special character        # in input string, add it        if( s == 0 ):                           # Generate random integer under string length                pos = random.randint(0,len(st)-1)                 # Generate random integer under 7 for                # indexing of special characters                st = insert(st, pos, low_case[random.randint(0,6)])         return st # generate_password function : This function is used to check# strength and if input string is not strong, It will suggestdef generate_password(n, p):           # flag for lower case, upper case, special        # characters and need of more characters        l = 0; u = 0; d = 0; s = 0; need = 0         # Password suggestions        suggest = ''         for i in range(n):                           # Password suggestion                if( p[i].islower() ):                        l = 1                elif( p[i].isupper() ):                        u = 1                elif( p[i].isdigit() ):                        d = 1                else:                        s = 1         # Check if input string is strong that        # means all flags are active        if( (l + u + d + s) == 4):                print("Your Password is Strong")                return        else:                print("Suggested Passwords")         # Suggest 10 strong strings        for i in range(10):                suggest = suggester(l, u, d, s, p)                need = 8 - len(suggest)                 if(need > 0):                        suggest = add_more_char(suggest, need)                print(suggest)  # Driver Codeinput_string = 'geek@2018' generate_password( len(input_string), input_string)  # This code is contributed by Arjun Saini
// C# code to suggest strong passwordusing System;using System.Collections.Generic; class GFG {     // adding more characters to suggest    // strong password    static string add_more_char(string str, int need)    {        int pos = 0;        Random randm = new Random();                 // all 26 letters        string low_case = "abcdefghijklmnopqrstuvwxyz";         for (int i = 0; i < need; i++) {            pos = randm.Next(1,1000) % str.Length;            str = str.Insert(pos,low_case[randm.Next(1000)                                        % 26].ToString());        }        return str;    }     // make powerful string    static string suggester(int l, int u, int d, int s,                                             string str)    {        Random randm = new Random();                 // all digits        string num = "0123456789";         // all lower case, uppercase and special        // characters        string low_case = "abcdefghijklmnopqrstuvwxyz";        string up_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";        string spl_char = "@#$_()!";         // position at which place a character        int pos = 0;         // if there is no lowercase char in input        // string, add it        if (l == 0) {                         // generate random integer under            // string length            pos = randm.Next(1,1000) % str.Length;             // generate random integer under 26 for            // indexing of a to z            str = str.Insert(pos,low_case[randm.Next(1,                                 1000) % 26].ToString());        }         // if there is no upper case char in input        // string, add it        if (u == 0) {            pos = randm.Next(1,1000) % str.Length;            str = str.Insert(pos,up_case[randm.Next(                              1,1000) % 26].ToString());        }         // if there is no digit in input string, add it        if (d == 0) {            pos = randm.Next(1,1000) % str.Length;            str = str.Insert(pos,num[randm.Next(1,1000)                                      % 10].ToString());        }         // if there is no special character in input        // string, add it        if (s == 0) {            pos = randm.Next(1,1000) % str.Length;            str = str.Insert(pos,spl_char[randm.Next(                              1,1000) % 7].ToString());        }         return str;    }     /* make_password function :This function is used    to check strongness and if input string is not    strong, it will suggest*/    static void generate_password(int n, string p)    {                 // flag for lower case, upper case, special        // characters and need of more characters        int l = 0, u = 0, d = 0, s = 0, need = 0;         // password suggestions.        string suggest;         for (int i = 0; i < n; i++) {                         // password suggestions.            if (p[i] >= 97 && p[i] <= 122)                l = 1;            else if (p[i] >= 65 && p[i] <= 90)                u = 1;            else if (p[i] >= 48 && p[i] <= 57)                d = 1;            else                s = 1;        }         // Check if input string is strong that        // means all flag are active.        if ((l + u + d + s) == 4) {            Console.WriteLine("Your Password is Strong\n");            return;        }        else            Console.WriteLine("Suggested password\n ");         /*suggest 10 strong strings */        for (int i = 0; i < 10; i++) {            suggest = suggester(l, u, d, s, p);            need = 8 - suggest.Length;            if (need > 0)                suggest = add_more_char(suggest, need);            Console.WriteLine(suggest + "\n");;        }    }     // Driver code    public static void Main()    {        string input_string = "geek@2018";        generate_password(input_string.Length, input_string);    }} // This code is contributed by Manish Shaw (manishshaw1)
<?php// php code to suggest// strong password // adding more characters to// suggest strong passwordfunction add_more_char( $str, $need){    $pos = 0;     // all 26 letters    $low_case = "abcdefghijklmnopqrstuvwxyz";     for ($i = 0; $i < $need; $i++)    {        $pos = rand() % strlen($str);        $str[$pos]=$low_case[rand() % 26];    }    return $str;} // make powerful stringfunction suggester($l, $u, $d, $s, $str){         // all digits    $num = "0123456789";     // all lower case, uppercase and    // special characters    $low_case = "abcdefghijklmnopqrstuvwxyz";    $up_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";         // $spl_char1 = "@#$_()!";    // position at which place    // a character    $pos = 0;     // if there is no lowercase char    // in input string, add it    if ($l == 0)    {                 // generate random integer        // under string length        $pos = rand() % strlen($str);         // generate random integer under        // 26 for indexing of a to z        $str[$pos]=$low_case[rand() % 26];    }     // if there is no upper case    // char in input string, add it    if ($u == 0)    {        $pos = rand() % strlen($str);        $str[$pos]=$up_case[rand() % 26];    }     // if there is no digit    // in input string, add it    if ($d == 0) {        $pos = rand() % strlen($str);        $str[$pos]=$num[rand() % 10];    }     // if there is no special character    // in input string, add it    if ($s == 0)    {        $pos = rand() % strlen($str);        $str[$pos]=$spl_char1[rand() % 7];    }     return $str;} // Make_password function : This// function is used to check// strongness and if input string// is not strong, it will suggestfunction generate_password($n, $p){         // flag for lower case, upper case,    // special characters and need    // of more characters    $l = 0;    $u = 0;    $d = 0;    $s = 0;    $need = 0;     // password suggestions.    $suggest;     for ($i = 0; $i < $n; $i++)    {        // password suggestions.        if ($p[$i] >= 97 && $p[$i] <= 122)            $l = 1;        else if ($p[$i] >= 65 && $p[$i] <= 90)            $u = 1;        else if ($p[$i] >= 48 && $p[$i] <= 57)            $d = 1;        else            $s = 1;    }     // Check if input string is strong    // that means all flag are active.    if (($l + $u + $d + $s) == 4)    {        echo "Your Password is Strong.\n";        return;    }    else        echo "Suggested password";     // suggest 10 strong strings    for ($i = 0; $i < 10; $i++)    {        $suggest = suggester($l, $u, $d, $s, $p);        $need = 8 - strlen($suggest);        if ($need > 0)            $suggest = add_more_char($suggest, $need);        echo "\n".$suggest;    }}     // Driver code    $input_string = "geek@2018";    srand(mktime(NULL));     // srand function set Seed for rand().    generate_password(strlen($input_string),                      $input_string); // This code is contributed by mits?>
Suggested password 
geek@201K8
geek@201S8
gOeek@2018
geek@201N8
geek@2P018
geek@D2018
geUek@2018
geek@2Q018
geek@2F018
geekZ@2018
 
Time complexity : O(n).  
Mithun Kumar
manishshaw1
arjunsaini9081
simranarora5sos
singghakshay
C++ Programs
Randomized
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Header files in C/C++ and its uses
Sorting a Map by value in C++ STL
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
Shallow Copy and Deep Copy in C++
K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
Shuffle a given array using Fisher–Yates shuffle Algorithm
Shuffle or Randomize a list in Java
Generating Random String Using PHP
Estimating the value of Pi using Monte Carlo