Skip to content

Commit a1dec46

Browse files
committed
Password Strength in Java
1 parent 2140235 commit a1dec46

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Diff for: Java/Password_Strength/PasswordStrength.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package password;
2+
3+
import java.util.Scanner;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
6+
7+
public class PasswordStrength {
8+
9+
public static void main(String[] args) {
10+
// TODO Auto-generated method stub
11+
Scanner sc = new Scanner(System.in);
12+
System.out.println("Enter the password:");
13+
String password = sc.next();
14+
check(password);
15+
}
16+
17+
public static void check(String password) {
18+
// TODO Auto-generated method stub
19+
// ^ represents the starting of the string.
20+
// (?=.*[a-z]) represent at least one lowercase character.
21+
// (?=.*[A-Z]) represents at least one uppercase character.
22+
// (?=.*\\d) represents at least one numeric value.
23+
// (?=.*[-+_!@#$%^&*., ?]) represents at least one special character.
24+
// . represents any character except line break.
25+
// + represents one or more times.
26+
String regex = "^(?=.*[a-z])(?=."+ "*[A-Z])(?=.*\\d)"+ "(?=.*[-+_!@#$%^&*., ?]).+$";
27+
28+
// Compile the ReGex
29+
Pattern pattern = Pattern.compile(regex);
30+
31+
// if the string entered is null then the output will be password invalid
32+
if (password == null) {
33+
System.out.println("Password Invalid.");
34+
return;
35+
}
36+
37+
// Find match between given string & regular expression
38+
Matcher matcher = pattern.matcher(password);
39+
40+
if (matcher.matches())
41+
System.out.println("Password Valid and Strong.");
42+
else
43+
System.out.println("Weak Password.");
44+
}
45+
46+
}

Diff for: Java/Password_Strength/Readme.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Password Strength
2+
3+
A program to check the strength of the password . For a strong password, the password must contain a mixture of upper case letters, a digit (including 0-9), and special characters with lower case letters.
4+
5+
## Sample Input
6+
Sapna123@
7+
8+
## Sample Output
9+
Password Valid and Strong.
10+
11+
## Screenshot
12+
![demo](https://user-images.githubusercontent.com/56690856/98788220-90b3ed00-2426-11eb-94e6-c9115c61eebf.png)
13+
14+
## Time Complexity
15+
O(N)
16+
17+
## Space Complexity
18+
O(1)

0 commit comments

Comments
 (0)