Java Program to Shuffle Characters in a String Without Using Shuffle() Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 4 Likes Like Report 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 String1. Using a Fisher-Yates AlgorithmBelow 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); } } OutputOriginal 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 StringBuilderWe 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); } } OutputOriginal 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. Create Quiz Comment P pankajbind Follow 4 Improve P pankajbind Follow 4 Improve Article Tags : Java Java Programs Java-Strings Java Examples Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like