CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Dec 2007
    Posts
    41

    Exclamation C++ randomizing string help

    Can someone please tell me or show me how to take a word and randomize it. Like to take the word moo and randomize it to make it oom and so on. I am new to programming and would like to make a word descrambler where it takes a word and compares it to a text file then randomize it and checks again until it finds the right word. I have already made it so that it checks the file for the word but I cant figure out how to randomize the word. I don't no if I'm supposed to change it into a char or what. Thanks to all that help.

    O ya im using Dev C++ and on windows vista if that helps. Thanks
    Last edited by Hellboyz; October 5th, 2008 at 06:22 PM.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: C++ randomizing string help

    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    
    void print_all_permutations(std::string s)
    {
        std::sort(s.begin(), s.end());
        do
        {
            std::cout << s << std::endl;
        } while (std::next_permutation(s.begin(), s.end()));
    }//print_all_permutations
    
    
    int main()
    {
        print_all_permutations("dog");
        return 1;
    }//main
    gg

  3. #3
    Join Date
    Dec 2007
    Posts
    41

    Re: C++ randomizing string help

    Thanks a lot for helping but this prints all of them at once. Is there a way to make this print only one so that i can compare it and if there is not a match then go on to the next? Thanks a lot for helping again .

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: C++ randomizing string help

    ^I should think the method would be fairly apparent from the above code sample.

    While I'm a big preponent of the std::string class, let's face it, they don't have anything as funny as this:
    The function below addresses the perennial programming quandary: “How do I take good data in string form and painlessly turn it into garbage?” This is actually a fairly simple task for C programmers who do not use the GNU C library string functions, but for programs based on the GNU C library, the strfry function is the preferred method for destroying string data.

  5. #5
    Join Date
    Dec 2007
    Posts
    41

    Re: C++ randomizing string help

    Ya it would be good if i could get it to print all the possibilities to a text file and just compare the text files but I cant get to seem to make it do that. I try adding the ofstream which i learned from cplusplus.com but i just get a whole bunch of errors every time I try to change anything in his code.

  6. #6
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: C++ randomizing string help

    You should be aware that the algorithm you're proposing is, in a word, slow. Consider the word "algorithm", of which there are 9! = 36,2880 permutations. Would you want to take each of those permutations and compare it (string comparison has its own complexity, too) against each word in a dictionary file?

    How about you tally the number of times each letter of the alphabet occurs in a word, and compare that to a similar count for each word in your dictionary.
    - Alon

  7. #7
    Join Date
    Nov 2003
    Posts
    1,405

    Re: C++ randomizing string help

    Quote Originally Posted by Hellboyz
    Like to take the word moo and randomize it to make it oom and so on.
    To randomize a word - shuffle it. There's a very simple standard algoritm available for that. You'll find it if you search the net.

  8. #8
    Join Date
    Dec 2007
    Posts
    41

    Re: C++ randomizing string help

    I always do a lot of searching before posting anything. But I can't find any algorithm for shuffling up a string and thanks Hermit I thought that it wouldn't take that long because of how fast a computer could go but Ill just say exactly what I'm trying to do. I've read some books on programming in C++ and I'm still learning but if any of you know what Hack this site is they have some programming challenges. I'm trying to actually accomplish this one the hard way by doing it myself rather then just using someone else's program to do it for me. What I have done so far is to assign all my scrambled words to variables and run through the program to check if they are a match. I just can't figure out how to randomize the strings yet. So with that said what kind of function or thing would I do for my specific situation? Thanks.

  9. #9
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: C++ randomizing string help

    Were you after something like this?

    http://www.cppreference.com/wiki/stl...random_shuffle

    You should be able to look at the source to get an idea of how it is done.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: C++ randomizing string help

    Quote Originally Posted by Hermit
    How about you tally the number of times each letter of the alphabet occurs in a word, and compare that to a similar count for each word in your dictionary.
    That would be a *much* faster way to tell if two words are identical "up to permutations".

    I thought that it wouldn't take that long because of how fast a computer could go
    Computer speed is great. Complexity theory is still incredibly important, though. A combinatorial function grows *much* faster than processor speed increases.

  11. #11
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: C++ randomizing string help

    What kind of suggested combinations or permutations to code by a stupid student ?

    What algorithm using by STL ?

    For instance, std::sort() used IntroSort() - QuickSort, HeapSort and InsertionSort, I mean what algorithm in random_shufftle, next_permutation and random_sample ?

    I just want to learn the algorithm and coded it myself.

    A billion thanks for your help.

  12. #12
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: C++ randomizing string help

    Please help newbie.

  13. #13
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: C++ randomizing string help

    Here is the source for HP's random_shuffle
    Code:
    template <class RandomAccessIterator, class Distance>
    void __random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
                          Distance*)
    {
       if (first == last) return;
       for (RandomAccessIterator i = first + 1; i != last; ++i) 
     #ifdef __STL_NO_DRAND48
         iter_swap(i, first + Distance(rand() % ((i - first) + 1)));
     #else
         iter_swap(i, first + Distance(lrand48() % ((i - first) + 1)));
     #endif
    }
    
    template <class RandomAccessIterator>
    inline void random_shuffle(RandomAccessIterator first,
                                RandomAccessIterator last)
    {
       __random_shuffle(first, last, distance_type(first));
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  14. #14
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: C++ randomizing string help

    What is the algorithm used by this STL ?

  15. #15
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: C++ randomizing string help

    What is the algorithm used by this STL ?
    From what I see, that standard library implementation uses (a variant of) Knuth shuffle, which is known by other, perhaps more accurate, names.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured