Created
November 5, 2012 07:58
-
-
Save anonymous/4015911 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String makeRandom(String content) | |
{ | |
if(content == null) | |
throw Exception("Null input vlaue"); | |
if(content.length <= 3) | |
return content; | |
else | |
return generateRadom(content); | |
} | |
String generateRandom(String content) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
String[] wordList = content.split(" "); | |
for(String word: wordList) | |
{ | |
builder.append(randomizeWord(word)); | |
builder.append(' '); | |
} | |
return builder.toString(); | |
} | |
String randomizeWord(String word) | |
{ | |
if(word.length <= 3) | |
return word; | |
else | |
{ | |
StringBuilder builder = new StringBuilder(word); | |
Random rnd = new Random(); | |
for(int i = 1; i < (word.length - 1) / 2; i++) | |
{ | |
int randInt = rnd.nextInt(word.length - i) + 1; | |
swap(builder, i, randInt); | |
} | |
} | |
} | |
void swap(StringBuilder builder, int i, int j) | |
{ | |
Char temp = builder[i]; | |
builder[i] = builder[j]; | |
builder[j] = temp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment