File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
src/main/java/com/thealgorithms/strings Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments