algorithm to scramble words C++

Started by
3 comments, last by mgarriss 18 years, 10 months ago
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
Advertisement
// NOT TESTED!!

#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!
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!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
not by much :)

This topic is closed to new replies.

Advertisement