14 semicolon anagram finder

Now that I have a little time to do this, Phoffric suggested to me to try doing this with stl, I have this so far.  But it only prints one set of words, my logic is flawed I think, this is NOT an assignment, just something for my own personal information and a little self teaching, we aare not allowed to use stl in class.

My output:
tab
bat
Press any key to continue . . .
My list;
bat care diet edit tab
#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std ;

int main()
{
  multimap<string,string> lookup ;
  ifstream file( "C:\\Users\\Mimi\\Desktop\\words[1].txt" ) ;
  string words ;
  while( file >> words )
  {
    string keys = words ;
    sort( keys.begin(), keys.end() ) ;
    lookup.insert( pair<string,string>(keys,words) ) ;
	
  }

  {

    string anagram=words ;
    sort( anagram.begin(), anagram.end() ) ;
    typedef multimap<string,string>::iterator iterator ;
    pair<iterator,iterator> bounds = lookup.equal_range(anagram) ;
    for( iterator iter = bounds.first ; iter != bounds.second ; ++iter )
      cout << iter->second << '\n' ;
  }

}

Open in new window

BrainDeadStudentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
The while loop inserts all words into a multimap.

The code after the while loop, however, is only run once, and uses the last word read from the file. You should probably put that second part in a loop too, and iterate over all words.
mkeiwuaCommented:
i agree, my question was going to be: what does the loop do when it reaches the space between bat and care.

B Rgds,

Mkeiwua
BrainDeadStudentAuthor Commented:
Hi, I need a little more dirrection, put the second part in a loop, a while, a for?  I was looking at for each but I don't seem to be able to get a handle on it.  Phoffric said this would be easy now that I understood how to do it from scratch but I am stumped.  Stl seems very very handy if I can learn to make it work properly.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Infinity08Commented:
>> Hi, I need a little more dirrection, put the second part in a loop, a while, a for?

The type of loop is secondary. First comes what it is you want to do. Clearly define what should happen, and then implement it :)
phoffric\Commented:
If you need a project with a good definition, here is what I did. I downloaded a scrabble word dictionary:
    12dicts-5.0.zip from  http://wordlist.sourceforge.net/

Then I wanted to find out the largest group of anagrams that can be found in this list. I found that there was a group of 7 words that were anagrams of each other:

      pares parse pears rapes reaps spare spear

There may be other groups of 7, but 7 is the largest group.

Naturally, you will want to start out with a set of much less words (say 10-20) for testing.
BrainDeadStudentAuthor Commented:
Phoffric,
I do not understand a word of what is on that page, I am still working on the loop thing,
But look how far I got on my own! Yay. . .look what I can do!
I am slow but I am getting there.
phoffric\Commented:
Oh you don't have to read that page and understand anything. Just search for 12dicts-5.0.zip - it is a link. If you hit this link then you can download this zip file. It has a txt file that has many scrabble words. With a file this big, you could, time permitting, try experimenting with different algorithms (perhaps one that you wrote earlier) and see which ones run faster.

I am impressed with how far you have come.

One other mini-project is to print out all the groups of anagrams (but that would be extensive if using the scrabble list.
phoffric\Commented:
I'm looking forward to seeing a working solution when I get back in about 8-10 hours.
Infinity08Commented:
Well, you're overthinking it. Finding anagrams is a relatively simple task.

So, just write down in simple words (some kind of pseudo code if you want) what it is that should happen. Then it's just a matter of finding the right language feature to implement it.

What is the basic idea of your algorithm ? Work from there ...
BrainDeadStudentAuthor Commented:
Dear Infinity08,
I can see that you are a genius, I, however am a begginner and a little old lady so it is not so easy for me.  I have come very very far since I started and I feel very very good about myself for doing it.  Now that I feel positively stupid for the moment, just trying to TRY something new, you will parden me if I am over thinking it.  This is not homework or an assignment, we are 1 week from the end of semester and I had a little free time and I thought I would take Phoffric up on his suggestion that this could be done with 14 semicolons if I used STL, which is not allowed in our class.  It is for my own entertainment but I am not finding it very entertaining at the moment because I am "Overthinking it " apparently!!!
Infinity08Commented:
What I said was not meant as criticism, or to make you feel bad.

It was meant to help you get the best result.

The code you posted - although a good start - is more complex than it needs to be. As I said : finding anagrams is relatively simple, so the code for it should be relatively simple too.

The way to get to that simple code, is by taking things step by step. First, start by thinking about how you would solve this problem efficiently (in your head, or on a piece of paper - ie. no code involved). Once you have a good image of how you would do it, you can try formalizing it in pseudo code. Once you are happy with the pseudo code, and can think of no further ways of simplifying it, you can translate the pseudo code into real code. It's only at this last stage that you should think about which language features you would use.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BrainDeadStudentAuthor Commented:
Ok, Seems I was having a LBS episode(low blood sugar)sorry for the intrusion of my other reality!
 I believe that I need to compare the keys and if they match print the words because they are already sorted
BrainDeadStudentAuthor Commented:
one step at a time is often good advice!
phoffric\Commented:
You figured it out (?)
Infinity08Commented:
>>  I believe that I need to compare the keys and if they match print the words because they are already sorted

Right. And that's something which is quite straightforward with the multimap you already have. Basically, the multimap contains this :

        "abt" -> "bat"
        "abt" -> "tab"
        "acer" -> "care"
        "deit" -> "diet"
        "deit" -> "edit"

Do you see an easy way to spot the anagrams ? What if you walk through the multimap from beginning to end ?


Btw, it seems you closed the question. That indicates you no longer need help with this. If this is not the case (ie. you'd still like us to help you with this), then please don't hesitate to continue working with us on it in this question.
phoffric\Commented:
@BrainDeadStudent,
Many ways to do this. I didn't use multimap; used map instead. Then I didn't have the case where there were two entries like:
        "deit" -> "diet"
        "deit" -> "edit"
since the key "deit" must be unique in a map. So that saved a step in that comparing keys becomes unnecessary.

Infinity08Commented:
>> Then I didn't have the case where there were two entries

I think the idea is to find all anagrams within the given words ... ie. you'd have to be able to say that "diet" and "edit" are anagrams.

You could still use a std::map for that, but then the value would have to be a vector or list or some other data structure that can hold more than one word (for the same key).
phoffric\Commented:
I looked back at what I did - the value was a set.
Infinity08Commented:
:)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.