Results 1 to 5 of 5
Thread: Scramble a String
- 07-15-2011, 10:07 PM #1
Scramble a String
I'm trying to scramble a String and I have it working, I was just wondering if there is a more efficient way of doing it than what I have. Does anyone have a suggestion on how to make this more efficient?
Java Code:String abc = "abcdefghijklmnopqrstuvwxyz"; String scrambled = ""; Random randGen = new Random(); while(scrambled.length() != abc.length()) { char rand = abc.charAt(randGen.nextInt(abc.length())); if(!scrambled.contains("" + rand)) scrambled += rand; }
- 07-15-2011, 10:39 PM #2
What is more efficient:
Fewer machine instructions executed
or fewer source statements.
Should the output String contain the same number of letters as the input String?
Your algorithm appears to only allow one copy of any letter. The longest that your output string could be would be 26 letters for any long string input to it.Last edited by Norm; 07-15-2011 at 10:41 PM.
- 07-15-2011, 10:49 PM #3
I have no idea which is more efficient... The output String should contain the exact characters of the input String just mixed up.
- 07-15-2011, 10:52 PM #4
Have you tested it with some different input to see what happens?The output String should contain the exact characters of the input String just mixed up.
I was asking you what you meant by being efficient. How do you determine if one piece of code is more efficient than another?I have no idea which is more efficient.
Which is worth more a one dollar bill or 30 quarters. The one dollar bill is more efficient to carry.
- 07-15-2011, 10:54 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
One thing I can say immediately is to use a stringbuilder instead of string concatenation. Then use append. Norm made some good points as well, this should probably take a string input and scramble it.
Similar Threads
-
Test for all empty Strings in LinkedHashMap<String,ArrayList<String>
By albertkao in forum New To JavaReplies: 1Last Post: 11-04-2010, 06:53 PM -
String builder scramble word game
By moncur in forum New To JavaReplies: 4Last Post: 10-22-2010, 03:14 AM -
Scramble Java Game
By equinox55 in forum New To JavaReplies: 3Last Post: 10-21-2010, 11:30 PM -
"Jumble" or "Scramble" Program
By Shadow22202 in forum Java AppletsReplies: 8Last Post: 04-30-2008, 03:42 AM -
Word Scramble
By lk9865 in forum New To JavaReplies: 5Last Post: 11-17-2007, 02:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks