algorithm to scramble words C++
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
// NOT TESTED!!
the c++ standard lib is our friend
#include <algorithm>void suffle_word ( std::string & word ) { std::random_shuffle( word.begin(), word.end() );}
the c++ standard lib is our friend
A very simple example might be:
std::string myString;
std::random_shuffle(myString.begin(), myString.end());
edit: ahh! u beat me!
std::string myString;
std::random_shuffle(myString.begin(), myString.end());
edit: ahh! u beat me!
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!
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!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement