I have an anagram program that if you type in a word's characters in any order, the dictionary file will be able to export it. However, my assignment is to build words that are shorter than the inputted search area in a one-to-one letter correspondance. An example of this would be inputting "abel's" and returning "abe, abe's, abel, abel's, able, ables", or whatever the words could fit the criteria.

I've had suggestion from my classmate that I should convert the string to a char, but I'm unsure if this is what I should perform (my test of this is commented out).

#include"WordClass.h"
#include<iostream>
#include<conio.h>
#include<vector>
#include<string>
#include<fstream>
#include<algorithm>
using namespace std;

int main()
{
    WordClass hold;
    bool output=false;
    ifstream inFile;
    string input, search, store, newin, newsea;
    //char chrArray[50], chrArray2[50];
    cout<<"Enter your search letters: ";
    getline(cin,input);
    newin=hold.sortletters(input);
    hold.lowercase(newin);
    hold.removenonletter(newin);
    /*for(int n=0; n<(int)input.length(); n++)
    {
        if(input[n]=='1')
        {
            chrArray[n]=input[n];
        }
    }*/
    cout<<endl;
    inFile.open("WordClassdictionaryfile.txt");
    while(!inFile.eof())
    {
        output=false;
        inFile>>search;
        store=search;
        newsea=hold.sortletters(search);
        hold.lowercase(newsea);
        hold.removenonletter(newsea);
        /*for(int n=0; n<(int)search.length(); n++)
        {
            if(search[n]=='1')
            {
                chrArray2[n]=search[n];
            }
        }*/
        if(hold.anagram(newin, newsea))
        {
            cout<<store<<endl;
        }
        else
        {
            continue;
        }
    }
    cout<<endl<<"Finished"<<endl;
    return 0;
    getch();
}

Anagram function in WordClass

bool WordClass::anagram(string newin, string newsea)
    {
        count=0;
        vector<string>word1;
        vector<string>word2;
        word1.push_back(newin);
        word2.push_back(newsea);
        if(newin.size()!=newsea.size())
        {
            return 0;
        }
        length=word1.size();
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<length; j++)
            {
                if(word1[i]==word2[j])
                {
                    count++;
                }
            }
        }
        if(count==length)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

Do your anagram words need to have meaning in dictionary?

Anyway, yes you need to work with the string as a char. You could do a brute-force way using permutation. For example, a given string is '123'. When you look for permutation of any length, you would get '1', '2', '3', '12', '13', '21', '23', '31', '32', '123', '132', '213', 231', '312', '321'.

I just wrote an annagram program simmilar to yours. My program didn't do the chopped up forms of a word. It only used the exact number of letters entered by the user. I wrote the input words into a struct that contained two items. The first was the word as entered by the user, the second was the alphbetized version. We then compared the sorted word against the sorted dictionary list of words. I think in your case you need to store every possible combination of the users input word and compare. Look up how to use permitations. I am not sure, but there could be a built in function that can do this.

STL has something that can help you: an algorithm that performs permutation. You can apply it to a string, so that you get different words each time, and than comparing to your inner dictionary to see if they form indeed a word. You can save these string in a vector/other form of container.

#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

int main(){
   string s="abel's";
   do{
      cout<<s<<endl;
   }while(next_permutation(s.begin(),s.end()));
}

You can also chop the string (saw you mentioned that it could be truncated), than search for it in your dictionary.

What would be the best way to utilize a char inside the next_permutation function? I try to run it but get errors C2228 and C2780 when using this

...
do{
    if(inputArray[50]==searchArray[50]
    {
        cout<<searchArray[50]<<endl;
    }while(next_permutation(inputArray[50].begin(),input.Array[50].end())); //error occurs here

    //error C2228: left of '.begin' must have class/struct/union
    //error C2228: left of '.end' must have class/struct/union
    //error C2780: 'bool std::next_permutation (_BidIt,_BidIt,_Pr)': expects 3 arguments - 2 provided

Inline Code Example Here

Use it with a container that accepts an interator, you can always put all your variables into a vector:

vector<int> array;

and after that you apply that next_permutation function over the array.

Would I be using this ^ in comparison to a string or a char? And what should I apply it to: the inputted letters, the dictionary search, or both?

When dealing with simple arrays:

int array[7];

you can use next_permutation like this:

#define SIZE 7
int main(){
   int array[SIZE]={1,2,3,4,5,6,7};
   do{
       for (size_t i=0;i<SIZE;i++)
           cout<<array[i]<<" ";
       cout<<endl;
   }while(next_permutation(array, array+SIZE));
   return 0;
}

but for strings I would just permute the string's characters, than I'll store them in a container and search in my dictionaries:

vector<string> permute(string a){
    vector<string> ret;
    //while loop
        //do permutations;
        ret.push_back(a);//which was permuted;
    //
    return ret;
}

Than I'll traverse the vector, and look each word in my dictionary.

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.