Jump to content
  • Advertisement
Sign in to follow this  

algorithm to scramble words C++

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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

Share this post


Link to post
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 this post


Link to post
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!