Horje
Java Program to Shuffle Characters in a String Without Using Shuffle()

Rearranging the order of characters, in a string can be done by shuffling them. Although the shuffle() method provided by Java’s Collections class is handy there are situations where you might require approaches. In this article, we will explore techniques, for shuffling characters in a string without using the shuffle() method.

Methods to shuffle characters in a String

1. Using a Fisher-Yates Algorithm

Below is the illustration of the Fisher-Yates Algorithm to shuffle characters in a String.

Java

// Java Program to Shuffle Characters in a String
// Using a Fisher-Yates Algorithm
import java.util.Random;
  
public class GFG {
    // Here we are shuffling the string using Fisher-Yates
    // algorithm
    public static String
    shuffleStringFisherYates(String str)
    {
        // Converting string to character array
        char[] chars = str.toCharArray();
        Random random = new Random();
        for (int i = chars.length - 1; i > 0; i--) {
            int j = random.nextInt(i + 1);
            char temp = chars[i];
            chars[i] = chars[j];
            chars[j] = temp;
        }
        return new String(chars);
    }
  
    public static void main(String[] args)
    {
        // this is the original string
        String originalString = "GeeksForGeeks";
  
        // shuffle the original string
        String shuffledString
            = shuffleStringFisherYates(originalString);
  
        // print both shuffled and original string
        System.out.println("Original string: "
                           + originalString);
        System.out.println(
            "Shuffled string (Fisher-Yates): "
            + shuffledString);
    }
}

Output

Original string: GeeksForGeeks
Shuffled string (Fisher-Yates): GkoGressekeFe


Explanation of the above Program:

  • The shuffleStringFisherYates() method convert the string to a character array.
  • After that it shuffles the string and returns the result.
  • In the main() method, the original string shuffles and it prints both the original and shuffled string.

2. Using StringBuilder

We can shuffle characters in a String using StringBuilder method.

Java

// Java Program to Shuffle Characters in a String
// Using a Fisher-Yates Algorithm
import java.util.Random;
  
// Driver Class
public class GFG {
  // shuffling the string using StringBuilder
    public static String shuffleStringStringBuilder(String str) {
        StringBuilder sb = new StringBuilder(str);
        Random random = new Random();
        
       // Iterating through StringBuilder from end to beginning
        for (int i = sb.length() - 1; i > 0; i--)  
        {
            int j = random.nextInt(i + 1);
            char temp = sb.charAt(i);
            sb.setCharAt(i, sb.charAt(j));
            sb.setCharAt(j, temp);
        }
        return sb.toString();
    }
  
    public static void main(String[] args) {
          // this is the original string
        String originalString = "GeeksForGeeks";
        String shuffledString = shuffleStringStringBuilder(originalString);
        
        // prints both the original and shuffled string
        System.out.println("Original string: " + originalString);
        System.out.println("Shuffled string (StringBuilder): " + shuffledString);
    }
}

Output

Original string: GeeksForGeeks
Shuffled string (StringBuilder): kFreessGGokee


Explanation of the above Program:

  • The shuffleStringStringBuilder() method takes a string as input and shuffles this using StringBuilder.
  • The method iterates through the StringBuilder from end to begining.
  • In the main() method, the original string shuffled and it prints both the original and shuffled string.



Reffered: https://www.geeksforgeeks.org


Java

Related
How to Declare an ArrayList with Values in Java? How to Declare an ArrayList with Values in Java?
Implement a Sparse Matrix Representation and Operations in Java Implement a Sparse Matrix Representation and Operations in Java
Java Program to Create a File with a Unique Name Java Program to Create a File with a Unique Name
How to Evaluate Math Expression Given in String Form in Java? How to Evaluate Math Expression Given in String Form in Java?
How to Split an ArrayList in Multiple Small ArrayLists? How to Split an ArrayList in Multiple Small ArrayLists?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14