Anagram program: words of lesser length

This is a discussion on Anagram program: words of lesser length within the C++ Programming forums, part of the General Programming Boards category; I have an anagram program that if you type in a word's characters in any order, the dictionary file will ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    12

    Anagram program: words of lesser length

    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).

    EDIT: The file is too large for the site.

    Anagram function:
    Code:
    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;
            }
        }
    Driver:
    Code:
    void 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;
        getch();
    }
    Last edited by hss1194; 12-12-2012 at 08:58 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > if(newin.size()!=newsea.size())
    If you want to make any lesser word, then you should probably have < or > in the test.
    != prevents all matches which could be a shorter length.

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    bool anagram(string newin, string newsea)
    {
        int count=0;
        if(newin.size()!=newsea.size())
        {
            return 0;
        }
        int length=newin.size();
        for(int i=0; i<length; i++)
        {
            for(int j=0; j<length; j++)
            {
                if(newin[i]==newsea[j])
                {
                    count++;
                }
            }
        }
        if(count==length)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
        
    int main()
    {
      cout << anagram("baba","abxx") << endl;
    }
    Also, you need to remove characters as you match them. You can't make baba out of abxx because you use each "ab" twice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    while(!inFile.eof())
    {
        output=false;
        inFile>>search;
        ...
    Don't use EOF tests to control you loops... (link points to example in C but the lesson is still applicable in C++) it is rarely done correctly. You should instead test the result of the read operation on the relevant file, ex:
    Code:
    while(inFile>>search)
    {
        output=false;
        ...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with an Anagram Program
    By Dro in forum C Programming
    Replies: 15
    Last Post: 02-15-2012, 07:34 AM
  2. anagram program help
    By pjr5043 in forum C++ Programming
    Replies: 50
    Last Post: 04-28-2008, 08:32 PM
  3. Replies: 7
    Last Post: 01-30-2006, 02:39 AM
  4. HELP!!! Big-O of my anagram program
    By Ham in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2001, 02:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21