Skip to content

Commit 8a4a210

Browse files
committed
Added PasswordGenerator Porgram in String Package
1 parent 421ac6d commit 8a4a210

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
package com.thealgorithms.strings;
3+
import java.util.Random;
4+
5+
/**
6+
* A simple password generator that creates random passwords
7+
* containing letters, digits, and special characters.
8+
*/
9+
public class PasswordGenerator {
10+
11+
public static String generatePassword(int length) {
12+
String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*";
13+
Random random = new Random();
14+
StringBuilder sb = new StringBuilder();
15+
16+
for (int i = 0; i < length; i++) {
17+
int index = random.nextInt(chars.length());
18+
sb.append(chars.charAt(index));
19+
}
20+
return sb.toString();
21+
}
22+
23+
// Example usage
24+
public static void main(String[] args) {
25+
System.out.println("Generated Password: " + generatePassword(12));
26+
}
27+
}

0 commit comments

Comments
 (0)