Saturday, September 21, 2024
Google search engine
HomeData Modelling & AIGenerate Passwords of given length

Generate Passwords of given length

Given an integer N, the task is to generate random passwords of length N of easy, medium and strong level each.

Easy level Password: Consists of only numbers or letters. Medium level Password: Consists of Letters as well as numbers. Strong level Password – Consists of Letters, numbers, and/or special characters.

Examples:

Input: N = 5 Output: Easy level password (only numbers): 98990 Easy password (only letters): tpFEQ Medium level password: b3bC8 Strong level password: 7`74n Input: N = 7 Output: Easy level password (only numbers): 7508730 Easy level password (only letters): mDzKCjN Medium level password: 4Z05s66 Strong level password: 2384Qu9

Approach: Follow the steps below to solve the problem:

  • For each password level, iterate through the given length.
  • To generate each password, randomly assign characters and numbers using Random Number Generation based on the password level.

Below is the implementation of the above approach: 

C




// C program to generate
// password of given length
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
// Function to generate easy level
// password with numbers
void easylevelpassnumbers(int n)
{
    int i;
 
    // Random character generation
    // setting the seed as TIME
    srand(time(NULL));
    printf("Easy level password "
        "(only numbers): ");
    for (i = 0; i < n; i++) {
 
        // rand() to assign random
        // characters in the password
        printf("%d", rand() % 10);
    }
    printf("\n");
}
 
// Function to generate easy level
// password with letters
void easylevelpassletters(int n)
{
    int i, d;
    printf("Easy level password"
        " (only letters): ");
    for (i = 0; i < n; i) {
        d = rand() % 123;
        if ((d >= 65 && d <= 90)
            || d >= 97) {
            printf("%c", (char)d);
            i++;
        }
    }
    printf("\n");
}
 
// Function to generate random
// medium level password
void midlevelpass(int n)
{
    int i, d;
    printf("Medium level password: ");
    for (i = 0; i < n; i++) {
        d = rand() % 123;
 
        // Random alphabetic characters
        if ((d >= 65 && d <= 90)
            || d >= 97) {
            printf("%c", (char)d);
        }
        else {
 
            // Random digits
            printf("%d", d % 10);
        }
    }
 
    printf("\n");
}
 
// Function to generate strong
// level password
void stronglevelpass(int n)
{
    int i, d;
    printf("Strong level password: ");
    for (i = 0; i < n; i++) {
        d = rand() % 200;
 
        // Random special characters
        if (d >= 33 && d <= 123) {
            printf("%c", (char)d);
        }
        else {
 
            // Random digits
            printf("%d", d % 10);
        }
    }
 
    printf("\n");
}
 
// Driver Code
int main()
{
    int n = 5;
 
    easylevelpassnumbers(n);
    easylevelpassletters(n);
    midlevelpass(n);
    stronglevelpass(n);
}


Python3




# Python program to generate
# password of given length
import random
import string
 
# Function to generate easy level password with numbers
def easy_level_pass_numbers(n):
    # Random character generation
    print("Easy level password (only numbers): ", end="")
    for i in range(n):
        # Random digits
        print(random.randint(0, 9), end="")
    print()
 
# Function to generate easy level password with letters
def easy_level_pass_letters(n):
    # Random character generation
    print("Easy level password (only letters): ", end="")
    for i in range(n):
        # Random alphabetic characters
        print(random.choice(string.ascii_letters), end="")
    print()
 
# Function to generate random medium level password
def mid_level_pass(n):
    # Random character generation
    print("Medium level password: ", end="")
    for i in range(n):
        # Random alphabetic characters or digits
        print(random.choice(string.ascii_letters + string.digits), end="")
    print()
 
# Function to generate strong level password
def strong_level_pass(n):
    # Random character generation
    print("Strong level password: ", end="")
    for i in range(n):
        # Random special characters or digits
        print(random.choice(string.ascii_letters + string.digits + string.punctuation), end="")
    print()
 
# Driver Code
if __name__ == '__main__':
    n = 5
 
    easy_level_pass_numbers(n)
    easy_level_pass_letters(n)
    mid_level_pass(n)
    strong_level_pass(n)
 
# Contributed by adityasha4x71


Output:

Easy level password (only numbers): 36707
Easy level password (only letters): cQWxF
Medium level password: 56G4w
Strong level password: 83s20

Time Complexity: O(N) 

Auxiliary Space: O(N)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments