-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassword program
More file actions
113 lines (108 loc) · 3.66 KB
/
Password program
File metadata and controls
113 lines (108 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//create a program of password
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
// A function to generate a random password of given length
char* generate_password(int length) {
// Define the characters that can be used in the password
char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=";
// Get the size of the chars array
int size = strlen(chars);
// Allocate memory for the password string
char* password = (char*)malloc((length + 1) * sizeof(char));
// Use a random seed based on the current time
srand(time(NULL));
// Loop through the length of the password
for (int i = 0; i < length; i++) {
// Generate a random index from 0 to size - 1
int index = rand() % size;
// Assign the corresponding character to the password string
password[i] = chars[index];
}
// Add a null terminator at the end of the password string
password[length] = '\0';
// Return the password string
return password;
}
// A function to check if a given password is valid or not
int check_password(char* password) {
// Define the minimum length of the password
int min_length = 8;
// Define the flags for different types of characters
int has_upper = 0;
int has_lower = 0;
int has_digit = 0;
int has_symbol = 0;
// Get the length of the password
int length = strlen(password);
// Loop through the password characters
for (int i = 0; i < length; i++) {
// Check if the character is an uppercase letter
if (password[i] >= 'A' && password[i] <= 'Z') {
has_upper = 1;
}
// Check if the character is a lowercase letter
else if (password[i] >= 'a' && password[i] <= 'z') {
has_lower = 1;
}
// Check if the character is a digit
else if (password[i] >= '0' && password[i] <= '9') {
has_digit = 1;
}
// Check if the character is a symbol
else {
has_symbol = 1;
}
}
// Return true if the password meets all the criteria, false otherwise
return (length >= min_length && has_upper && has_lower && has_digit && has_symbol);
}
// The main function of the program
int main() {
// Declare a variable to store the user's choice
int choice;
// Declare a variable to store the user's password
char* password;
do {
// Print the menu options
printf("Welcome to Password Security Program\n");
printf("1. Generate a random password\n");
printf("2. Check your own password\n");
printf("3. Exit\n");
printf("Enter your choice: ");
// Scan the user's choice
scanf("%d", &choice);
// Switch on the user's choice
switch (choice) {
case 1:
// Generate a random password of length 10 and print it
password = generate_password(10);
printf("Your random password is: %s\n", password);
// Free the memory allocated for the password
free(password);
break;
case 2:
// Ask the user to enter their own password and scan it
printf("Enter your own password: ");
scanf("%s", password);
// Check if the password is valid or not and print the result
if (check_password(password)) {
printf("Your password is valid.\n");
}
else {
printf("Your password is invalid.\n");
}
break;
case 3:
// Exit the program with a message
printf("Thank you for using Password Security Program. Goodbye!\n");
break;
default:
// Print an error message for invalid choice
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice !=3); // Repeat until the user chooses to exit
return(0);
}