Programm a function that scrambles a word in java
- User
Programm a function that scrambles a word in java
Assistant
Here's a Java function that scrambles (randomly rearranges) the letters in a word:
public static String scrambleWord(String word) { // Convert string to char array char[] letters = word.toCharArray(); // Fisher-Yates shuffle algorithm Random rand = new Random(); for (int i = letters.length - 1; i > 0; i--) { int j = rand.nextInt(i + 1); // Swap letters[i] and letters[j] char temp = letters[i]; letters[i] = letters[j]; letters[j] = temp; } // Convert back to string and return return new String(letters); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.