Jump to content

Scramble Words

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Elaine

Elaine

    Newbie

  • Members
  • Pip
  • 1 posts
Hi! I hope some can help me. I'm learning java on my own. I want to be able to scramble sentences, but I want to assign each letter with another letter of my choice. For example: Unscramble these words. It then would read nseubcralm esteh dswor. Thanks in advance.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
What I would do is:

[1]Split the string on every space using String.split(" ");
[2]loop trough every string in the array
[3]for each string (word), generate a random number between 0 and word.length();
[4]take the letter at the random position, and put it in a new string
[5]remove this letter from the word
[6]go to step 3

Tip, the StringBuilder class has a charAt(int index), and a deleteCharAt(int index) method.