-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRandomWordMaker.java
More file actions
44 lines (38 loc) · 1.5 KB
/
RandomWordMaker.java
File metadata and controls
44 lines (38 loc) · 1.5 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
import java.util.Scanner;
import java.util.Random;
import java.util.HashSet;
public class RandomWordMaker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word: ");
String wor = sc.next().toUpperCase();
int c = wor.length();
Random rG = new Random(); // Changed object name to rG
HashSet<String> WordList = new HashSet<>(); // Changed object name to WordList
// Generate all possible unique random words
while (WordList.size() < factorial(c)) {
StringBuilder randomWord = new StringBuilder(wor);
StringBuilder Shuf = new StringBuilder(); // Changed object name to Shuf
while (randomWord.length() > 0) {
int randomIndex = rG.nextInt(randomWord.length());
Shuf.append(randomWord.charAt(randomIndex));
randomWord.deleteCharAt(randomIndex);
}
WordList.add(Shuf.toString());
}
// Print all unique random words
System.out.println("Unique random words generated from the input word:");
int count = 1;
for (String word : WordList) {
System.out.println(count + " " + word);
count++;
}
}
// Helper method to calculate factorial
public static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
}