![]() |
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
Output
Original string: GeeksForGeeks Shuffled string (Fisher-Yates): GkoGressekeFe Explanation of the above Program:
2. Using StringBuilderWe can shuffle characters in a String using StringBuilder method. Java
Output
Original string: GeeksForGeeks Shuffled string (StringBuilder): kFreessGGokee Explanation of the above Program:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |