#Word Shuffle Program Code Review & Help
1 messages · Page 1 of 1 (latest)
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
the word "hello" could be rearranged to create the jumbled word "olhel".
Here is a sample code to achieve this:
import java.util.Random;
public class WordShuffle {
public static void main(String[] args) {
String word = "hello";
String jumbledWord = shuffleWord(word);
System.out.println("Original word: " + word);
System.out.println("Jumbled word: " + jumbledWord);
}
private static String shuffleWord(String word) {
Random random = new Random();
char[] letters = word.toCharArray();
for (int i = 0; i < letters.length; i++) {
int randomIndex = random.nextInt(letters.length);
// Swap letters at index i and randomIndex
char temp = letters[i];
letters[i] = letters[randomIndex];
letters[randomIndex] = temp;
}
return new String(letters);
}
}
In this code, the shuffleWord method takes a string as input, converts it into an array of characters, and then shuffles the characters randomly by swapping them at random indexes. The shuffled word is then returned as a string.
You can modify the word variable in the main method to test with different words. Let me know if you need any further clarification or help with this program!
If you require to see any of the methods that I have created, let me know and I will send them : )
Closed the thread.