Java Program to Shuffle Characters in a String Without Using Shuffle() Last Updated : 01 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article 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. Comment More infoAdvertise with us Next Article Java Program to Swap characters in a String P pankajbind Follow Improve Article Tags : Java Java Programs Java-Strings Java Examples Practice Tags : JavaJava-Strings Similar Reads Java Program to Convert String to Char Stream Without Using Stream Char stream defines the array of characters. In this article, we will learn the different types of methods for converting a String into a char stream in Java without using Stream. Let us see some methods one by one. ExamplesInput: String = HelloGeeksOutput: [H, e, l, l, o, G, e, e, k, s] Input: Stri 4 min read Java Program to Swap characters in a String The task at hand involves manipulating a string S of length N, given as input. The string is subjected to a series of B swaps, each performed according to the following procedure: The character at position i is swapped with the character located C positions ahead of it, or (i + C)%N. This swapping p 6 min read How to Shuffle Characters in a String in Java? In this article, we will learn how to shuffle characters in a String by using Java programming. For this, we have taken a String value as input and the method is available in java.util package and this method takes a list as input. Steps to shuffle characters in a String in JavaFirst, take one Strin 2 min read How to Convert String to Char Stream in Java without Using Library Functions? In Java, we can convert a string to a char stream with a simple built-in method .toCharArray(). Here, we want to convert the string to a char stream without using built-in library functions. So, we will create an empty char array iterate it with string characters, and store the individual characters 2 min read Java Program to Swap Corner Words and Reverse Middle Characters of a String Given a string containing n numbers of words. The task is to swap the corner words of the string and reverses all the middle characters of the string. Input: "Hello this is the GFG user" Output: "user GFG eth si siht Hello" Input: "Hello Bye" Output: "Bye Hello" Methods: Using the concept of ASCII v 5 min read Java program to swap first and last characters of words in a sentence Write a Java Program to Swap first and last character of words in a Sentence as mentioned in the example? Examples: Input : geeks for geeks Output :seekg rof seekg Approach:As mentioned in the example we have to replace first and last character of word and keep rest of the alphabets as it is. First 2 min read Like