Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I shuffle the letters of a word?

What is the easiest way to shuffle the letters of a word which is in an array? I have some words in an array and I choose randomly a word but I also want to shuffle the letters of it.

public static void main (String[] args ) {
    String [] animals = { "Dog" , "Cat" , "Dino" } ;    
    Random random = new Random();
    String word = animals [random.nextInt(animals.length)];

    System.out.println ( word ) ;
    //I Simply want to shuffle the letters of word     
}

I am not supposed to use that List thing. I've come up with something like this but with this code it prints random letters it doesn't shuffle. Maybe I can code something like do not print if that letter already printed?

//GET RANDOM LETTER
for (int i = 0; i< word.length(); i++ ) {

char c = (word.charAt(random.nextInt(word.length())));
System.out.print(c); } 
  }
like image 371
morgothraud Avatar asked Dec 12 '22 09:12

morgothraud


2 Answers

Really no need for collection and anything more than what follows:

public static void main(String[] args) {

    // Create a random object
    Random r = new Random();

    String word = "Animals";

    System.out.println("Before: " + word );
    word = scramble( r, word );
    System.out.println("After : " + word );
}

public static String scramble( Random random, String inputString )
{
    // Convert your string into a simple char array:
    char a[] = inputString.toCharArray();

    // Scramble the letters using the standard Fisher-Yates shuffle, 
    for( int i=0 ; i<a.length ; i++ )
    {
        int j = random.nextInt(a.length);
        // Swap letters
        char temp = a[i]; a[i] = a[j];  a[j] = temp;
    }       

    return new String( a );
}
like image 91
Rastikan Avatar answered Dec 28 '22 23:12

Rastikan


You can use Collections.shuffle :

List<Character> l = new ArrayList<>();
for(char c :  word.toCharArray()) //for each char of the word selectionned, put it in a list
    l.add(c); 
Collections.shuffle(l); //shuffle the list

StringBuilder sb = new StringBuilder(); //now rebuild the word
for(char c : l)
  sb.append(c);

word = sb.toString();

I am not supposed to use that List thing.

Then you can create two StringBuilder objects. One will hold the original word and one will create the shuffled one :

StringBuilder s = new StringBuilder(word);
StringBuilder wordShuffled = new StringBuilder();
while(s.length() != 0){
    int index = random.nextInt(s.length());
    char c = s.charAt(index);
    wordShuffled.append(c);
    s.deleteCharAt(index);
}
System.out.println(wordShuffled.toString());

I found something like deleteCharAt but I guess it works with StringBuilder or something. I cannot use that

Here you can find some nice utilities methods that permits to shuffle an array.

public static char[] shuffleArray(char[] x) {    
       for ( int i = x.length; i > 0; i-- ) {
            int rand = (int)(Math.random()*(i));
            char temp = x[i-1];
            x[i-1] = x[rand];
            x[rand] = temp;
        }
       return x;
}

Then just call this method and use the constructor String(char[] value) :

System.out.println(new String(shuffleArray(word.toCharArray())));

Next time clearly state, what you can use/not use.

like image 33
Alexis C. Avatar answered Dec 28 '22 22:12

Alexis C.