-
Notifications
You must be signed in to change notification settings - Fork 0
/
L2_T2.java
90 lines (77 loc) · 2.94 KB
/
L2_T2.java
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
//Description: Create a program that checks the strength of a password. Prompt the user to input a password and analyze its strength based on certain criteria, such as length, presence of uppercase letters, lowercase letters, numbers, and special characters. Provide feedback on the password strength.
import java.util.Scanner;
public class L2_T2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt the user to input a password
System.out.print("Enter your password: ");
String password = scanner.nextLine();
scanner.close();
// Check password strength
int strength = checkPasswordStrength(password);
// Provide feedback on the password strength
switch (strength) {
case 1:
System.out.println("Weak password. It should be at least 8 characters long.");
break;
case 2:
System.out.println("Moderate password. It should include uppercase letters.");
break;
case 3:
System.out.println("Strong password. It should include lowercase letters, numbers, and special characters.");
break;
case 4:
System.out.println("Very strong password. Good job!");
break;
}
}
private static int checkPasswordStrength(String password) {
int strength = 0;
// Check length
if (password.length() >= 8) {
strength++;
// Check for uppercase letters
if (containsUppercase(password)) {
strength++;
// Check for lowercase letters, numbers, and special characters
if (containsLowercase(password) && containsNumber(password) && containsSpecialCharacter(password)) {
strength++;
}
}
}
return strength;
}
private static boolean containsUppercase(String password) {
for (char c : password.toCharArray()) {
if (Character.isUpperCase(c)) {
return true;
}
}
return false;
}
private static boolean containsLowercase(String password) {
for (char c : password.toCharArray()) {
if (Character.isLowerCase(c)) {
return true;
}
}
return false;
}
private static boolean containsNumber(String password) {
for (char c : password.toCharArray()) {
if (Character.isDigit(c)) {
return true;
}
}
return false;
}
private static boolean containsSpecialCharacter(String password) {
String specialCharacters = "!@#$%^&*()-_=+[]{}|;:'\",.<>/?";
for (char c : password.toCharArray()) {
if (specialCharacters.contains(String.valueOf(c))) {
return true;
}
}
return false;
}
}