Question

I have been trying to make an anagram maker, using a textBox (encryption_text) for input, which the text is "Hello World", and the output textBox (encrypted_text) which receives the text:

"ellllloooo
WWWWWWooooooorrrrrrrrllllllllldddddddddd".

I also have a textBox called 'anag_used', which should record the used number/location in the string to encrypt.

Have I over complicated it, or is there an error?

Thanks :)

Here is my code:

void anagram()
         {
             string toanagram = marshal_as<string>(encryption_text->Text);
             string out;
             int k;
             System::String^ rndstr;
             System::String^ ktostr;
             ostringstream kstr;
             anag_used->Clear();
             for (int i = 0; i < toanagram.size(); ++i)
             {
                anag_used->Text += "\n";
                int rnd = 0 + rand() % toanagram.size();
                ostringstream rndtostr;
                rndtostr << rnd;
                rndstr = gcnew System::String(rndtostr.str().c_str());
                for (int l = 0; l < i; ++l)
                {
                if (anag_used->Lines[l] == rndstr)
                {
                    k = rnd;
                    kstr << k;
                    ktostr = gcnew System::String(kstr.str().c_str());
                    for (System::String^ j = anag_used->Lines[l]; j == ktostr; k = 0 + rand() % toanagram.size())
                    {
                        kstr << k;
                        ktostr = gcnew System::String(kstr.str().c_str());
                        if (anag_used->Lines[l] == ktostr)
                        {
                            //Do someting if you want
                        }
                        else
                        {
                            out += toanagram[k];
                            anag_used->Lines[l] = ktostr;
                        }
                    }
                }
                else
                {
                    out += toanagram[i];
                    anag_used->Lines[i] = rndstr;
                }
                }
             }
             encrypted_text->Text = marshal_as<System::String^>(out);
         }

EDIT: FOUND A MUCH SIMPLER WORKING CODE

#include <algorithm>

.

            string toanagram = marshal_as<string>(encryption_text->Text);
            sort(toanagram.begin(), toanagram.end());
            encrypted_text->Text = marshal_as<System::String^>(toanagram);
Was it helpful?

Solution

This works for console, but you could implement it in C++/CLI quite easily

#include <iostream>
#include <sstream>
#include <vector>
#include <ctime>

void str_vect(std::vector<const char>* v, std::string& s)
{
    for (int i = 0; i < s.length(); ++i)
    {
        v->push_back(s[i]);
    }
}

int main()
{
    for (;;)
    {
        std::cout << "Please enter the word / phrase\n";
        std::string word;
        std::getline(std::cin, word);
        std::vector<const char> word_split;
        str_vect(&word_split, word);
        int sz = word_split.size();
        std::string anagram;
        for (int i = 0; i < sz; ++i)
        {
            srand(time(NULL));
            int r = (rand() % (word_split.size() - 0)) + 0;
            anagram += word_split[r];
            word_split.erase((word_split.begin()) + r);
        }
        system("cls");
        std::cout << "Please guess the anagrammed phrase / word - '" << anagram << "'\n";
        int max_tries = 3;
        int tries = max_tries;
        for (int i = 0; i <= max_tries; ++i)
        {
            std::string guess;
            std::getline(std::cin, guess);
            if (guess != word)
            {
                tries--;
                if (tries == 0)
                {
                    std::cout << "You have ran out of tries. The answer was: " << word << "\n";
                    break;
                }
                std::cout << tries << ((tries == 1) ? " try" : " tries") << " left\n";
            }
            else
            {
                std::cout << "Correct!\n";
                break;
            }
        }
    }
}

OTHER TIPS

        #include <algorithm> 

.

        string toanagram = marshal_as<string>(encryption_text->Text);
        sort(toanagram.begin(), toanagram.end());
        encrypted_text->Text = marshal_as<System::String^>(toanagram);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top