algorithm to scramble words C++

This topic is 4935 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi there I need an algorithm to scramble words from a text loaded from a txt file. I need a good one very quickly, because I don't have much time to work on this. Does anyone know of any algorithms for this purpose? Thanks in advance

Share on other sites
// NOT TESTED!!

#include <algorithm>void suffle_word ( std::string & word ) {  std::random_shuffle( word.begin(), word.end() );}

the c++ standard lib is our friend

Share on other sites
A very simple example might be:

std::string myString;
std::random_shuffle(myString.begin(), myString.end());

edit: ahh! u beat me!

Share on other sites
Silent Angel,

Are you just looking for an algorithm that randomly distributes the characters in a text message, or are you looking for some kind of encryption algorithm that allows you to return to the original message?

Also, if you just want to randomly distribute the characters do you want to scatter the letters within a word, or scatter the letters across all words? In other words, do you want the boundries between words preserved or does it matter?

If all you want to do is randomly scatter the characters of a text message anywhere within the message, not preserving spaces, and not with the intention of ever returning to the original message, then mgarriss' little code snipet should work great. Otherwise, you might need to post a bit more information.

Cheers, Mate!

not by much :)

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• Forum Statistics

• Total Topics
633662
• Total Posts
3013231
×