Skip to content

Instantly share code, notes, and snippets.

Created November 5, 2012 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save anonymous/4015911 to your computer and use it in GitHub Desktop.
Save anonymous/4015911 to your computer and use it in GitHub Desktop.
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