0% found this document useful (0 votes)
2 views8 pages

Ramdom Password

The document describes a Random Password Generator implemented in C language, which allows users to specify the length and character sets for the password. It includes functions to validate input and generate a password based on selected criteria such as lowercase letters, uppercase letters, digits, and symbols. The program also provides usage instructions and prompts the user for input to customize the password generation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Ramdom Password

The document describes a Random Password Generator implemented in C language, which allows users to specify the length and character sets for the password. It includes functions to validate input and generate a password based on selected criteria such as lowercase letters, uppercase letters, digits, and symbols. The program also provides usage instructions and prompts the user for input to customize the password generation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

TOPIC

RANDOM PASSWORD GENETOR IN C LANGUAGE

BY

NAME MATRIC. NO

Lecturer in charge: Date:


#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <stdbool.h>

#include <string.h>

#include <ctype.h>

#define MIN_LENGTH 6

#define MAX_LENGTH 64

// Character sets

const char lowercase[] = "abcdefghijklmnopqrstuvwxyz";

const char uppercase[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

const char digits[] = "0123456789";

const char symbols[] = "!@#$%^&*()_-+=[]{}|;:,.<>?";

void generate_password(int length, bool use_lower, bool use_upper, bool use_digits, bool use_symbols)
{

if (length < MIN_LENGTH || length > MAX_LENGTH) {

printf("Password length must be between %d and %d\n", MIN_LENGTH, MAX_LENGTH);

return;

if (!use_lower && !use_upper && !use_digits && !use_symbols) {

printf("At least one character set must be selected\n");

return;

// Build the character pool based on selected options

char pool[256] = {0};


int pool_size = 0;

if (use_lower) {

strcat(pool, lowercase);

pool_size += strlen(lowercase);

if (use_upper) {

strcat(pool, uppercase);

pool_size += strlen(uppercase);

if (use_digits) {

strcat(pool, digits);

pool_size += strlen(digits);

if (use_symbols) {

strcat(pool, symbols);

pool_size += strlen(symbols);

// Generate password

char password[MAX_LENGTH + 1] = {0};

for (int i = 0; i < length; i++) {

int index = rand() % pool_size;

password[i] = pool[index];

printf("Generated Password: %s\n", password);

}
void print_help() {

printf("Random Password Generator\n");

printf("Usage:\n");

printf(" Enter desired password length (between %d and %d)\n", MIN_LENGTH, MAX_LENGTH);

printf(" Select character sets to include:\n");

printf(" - lowercase letters (a-z)\n");

printf(" - uppercase letters (A-Z)\n");

printf(" - digits (0-9)\n");

printf(" - symbols (!@#$ etc.)\n");

printf("\nAt least one character set must be selected.\n");

int main() {

// Seed the random number generator

srand(time(NULL));

int length;

bool use_lower = false, use_upper = false, use_digits = false, use_symbols = false;

char input[10];

print_help();

// Get password length

printf("\nEnter password length (%d-%d): ", MIN_LENGTH, MAX_LENGTH);

if (fgets(input, sizeof(input), stdin)) {

length = atoi(input);

} else {

length = 0;

}
// Get character set options

printf("Include lowercase letters? (y/n): ");

if (fgets(input, sizeof(input), stdin) && tolower(input[0]) == 'y') {

use_lower = true;

printf("Include uppercase letters? (y/n): ");

if (fgets(input, sizeof(input), stdin) && tolower(input[0]) == 'y') {

use_upper = true;

printf("Include digits? (y/n): ");

if (fgets(input, sizeof(input), stdin) && tolower(input[0]) == 'y') {

use_digits = true;

printf("Include symbols? (y/n): ");

if (fgets(input, sizeof(input), stdin) && tolower(input[0]) == 'y') {

use_symbols = true;

// Generate and print the password

generate_password(length, use_lower, use_upper, use_digits, use_symbols);

return 0;

}
Input
Output

You might also like