I'm currently working on a project similar to that of text twist.
I have a dictionary txt file which I stored into an array.
I then have user input a guess, then compare the guess to the dictionary array using a for loop. Somewhere along the lines of this:

string guess;
cin >> guess;

dictionary(guess);


void dictionary(string guessF, dictionaryarray[])
{
     bool check = false;
     for(i = 0; i < 10000; i++)
     {
          if(guessF == dictionaryarray[i])
          {
            cout<<"correct!";
            check = true;
            break;
          }
     }
     if(check == false)
     {
          cout<<"Wrong!";
     }

Thats just the quick gist of it (I also added parameters to check if the guess is < 3 char or guess > 6). And it works, when I input a word that isn't in the dictionary, or less than 3 or more than 6 characters, it couts the correct lines. However, if I input ANYTHING that is in the dictionary.txt, it returns correct.

So my question is how do I compare the given word, say givenword = "monkey";

in the dictionary there is say for example "key" , "money", "donkey", "monk";

I want to be able to run a function that goes through the dictionary and pulls out "key", "money", and "monk" as those are words that can be formed and stores them in a 3rd array.
and ignores "donkey" because there is no "d" in givenword.

I hope I made some sense in my question =X

Much appreciated.

Recommended Answers

All 12 Replies

So you're saying you want to parse either the given word OR the words in the dictionary by the length of 3,4,5,6 and make new words for comparison?

I think so...just to clarify... I will have a total of 3 string arrays
first array is formed by a txt file containing six letter words. The program randomly pulls a word and passes it into main to make GIVENWORD.

second array is formed by a txt file containing dictionary.

third array will be created by comparing characters in GIVENWORD to words in dictionary, and if characters in GIVENWORD can form words in DICTIONARY array, pull that word out and store in third array.

That way I have a set number of possible formed words in the third array i can easily compare user GUESSES input to.

I hope this helps clear it up a bit.

Think about what it means "if characters in GIVENWORD can form words in DICTIONARY array". Break that down into smaller steps. Options include
1) Make every possible sub-word from GIVENWORD, and then see if that sub-word is in the dictionary.
2) For every word in the dictionary, see if you have the right letters in GIVENWORD to make it.
Either should work fine. Extra credit if you can get it to work both ways! A hint on the second method: put the letters of GIVENWORD into a temporary array, and then "remove" each as you use it to form the dictionary word -- instead of -actually- removing a letter, what could you replace it with so that it couldn't be accidentally re-used? E.g. you can make "TENT" from "TENANT", but you can't make "TEE" from "TENANT". Think about how you'd solve this problem on paper: "Can you make word A from the letters in word B?" And then translate that process into code.

Are you sure you are supposed to find if a word can be made from the letters and not simply find the dictionary word if it exists in the guess word?

IOW, if the word is CREATE you can certainly find EAT. But are you sure you need to find TAR RAT and ART too if they are in the dictionary?

okay so something like this?

bool checkpossible(string x, string y)
{
   for(int i = 0; i < a.length(); i++)
   {
      for(int p = 0; p < b.length(); p++)
      {
         if(x[i] == y[p])
         {
           y[p] = 0;
         }
   }

   for(int z = 0; z < b.length(); z++)
   {
      if(y[z] != 0)
        return false;
   }
   return true;
}

Where you specify a.length() and b.length(), do you mean x and y? And which is which? Please use more meaningful variable names so other people looking at your code don't have to guess what each variable represents.

I think in your loop of p, you need to handle what happens if you -don't- find the character you're looking for, but since I can't tell which word is which, I'll leave that to you.

Where you specify a.length() and b.length(), do you mean x and y? And which is which? Please use more meaningful variable names so other people looking at your code don't have to guess what each variable represents.

I think in your loop of p, you need to handle what happens if you -don't- find the character you're looking for, but since I can't tell which word is which, I'll leave that to you.

oh sorry about that

bool checkpossible(string givenword, string dictionaryword)
{
   for(int i = 0; i < givenword.length(); i++)
   {
      for(int p = 0; p < dictionaryword.length(); p++)
      {
         if(givenword[i] == dictionarywrod[p])
         {
           dictionaryword[p] = 0;
         }
      }
   }

   for(int z = 0; z < dictionaryword.length(); z++)
   {
      if(dictionaryword[z] != 0)
        return false;
   }
   return true;
}

Thanks for the clarification. There's still a logic error in your loop eliminating letters out of dictionaryword: what happens if a letter in givenword occurs more than once in dictionaryword, for example if givenword is "TENT" and dictionaryword is "TEE"?

Thanks for the clarification. There's still a logic error in your loop eliminating letters out of dictionaryword: what happens if a letter in givenword occurs more than once in dictionaryword, for example if givenword is "TENT" and dictionaryword is "TEE"?

umm, I don't quite understand. sorry.

Write down the givenword "TENT" in a horizontal sequence of 4 boxes, one letter per box, to emphasize the array-ness of the string. Do the same for dictionaryword "TEE". Follow your own code, one line at a time, doing exactly what it says. What result do you get? Is it correct? If not (hint, I wouldn't be asking you to do this otherwise), what do you need to change or add to make it work correctly?

Write down the givenword "TENT" in a horizontal sequence of 4 boxes, one letter per box, to emphasize the array-ness of the string. Do the same for dictionaryword "TEE". Follow your own code, one line at a time, doing exactly what it says. What result do you get? Is it correct? If not (hint, I wouldn't be asking you to do this otherwise), what do you need to change or add to make it work correctly?

ah!

p = b.length();

Yes, that's certainly one way to handle it, assuming you insert that line at the correct place in your code. If it's working now, please mark your thread as "solved". Good work on this!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.