0

I'm new to OOP and data structures. I'm playing around with a C++ algorithm to compare one string sentence with another. The idea is to extract individual words from both strings, store them in two different hash tables and determine with a boolean if the words of the first string appear in the second one. There must be many ways to solve this, but for my purposes the approach should use hash tables. I created a class called hashClass which has one constructor and three methods. The first method splits the string, the second hashes the words split and stores them at an index in the hash table and the third method should determine if the words of the first string (Magazine object) appear in the second one (Note object) have. I'm stuck at the third mainly because I don't know how to compare the contents stored at the hash tables in each object. Here is the code for my main.cpp:

int main()
{
    hashClass Magazine;
    hashClass Note;

    Magazine.SplitString("give me one grand today night");

    Note.SplitString("igev em eno drnag today night");

    bool IsMagReplica(const hashClass Magazine, const hashClass Note)
    {
     //what can I do here to compare the contents of both hash tables which are stored as objects of a class?
    }

    return 0;
}

Here is the code for my hash.h:

class hashClass
{
public:
    hashClass();
    void SplitString(string str);
    int HashWord(string word, int length);
    bool IsMagReplica(hashClass htMag, hashClass htNote);

private:

    static const int tableSize = 52;

    struct item
    {
        string word;
        int length;
        item* next;
    };

    item* HashTable[tableSize];

};

Let me know if you also need to take a look at the hash.cpp code. Any help is deeply appreciated. Hope my explanation is clear and concise. Thanks.

Joel Pou
  • 158
  • 2
  • 13
  • All you should have to do is iterate through all of the entries in the smaller of the two tables and check if they are in the larger. Have you considered using `std::map`, `std::unordered_map`, or better yet a `std::set` instead of custom-cooking your own? I ask because unless the point of the assignment is to create a hash table, you're wasting a lot of your time reimplementing thew wheel. In fact, your entire problem could possibly be implemented with `std::stringstream`, `std::getline` and `std::set` in somewhere around 20 lines of code. – user4581301 Mar 06 '17 at 21:16
  • Yeah, I know there are much more efficient ways to solve this, but as you may have suspected, the point of the assignment is to create hash tables. – Joel Pou Mar 06 '17 at 21:21

1 Answers1

0

Can't fit this in a comment, so here goes:

  1. Write the hashtable separately from the rest of the functionality the assignment requires. Don't add any special functions to it so that you can use the hashtable class again in a futture assignment. This demonstrates to the instructor that you understand the fundamental concept of coupling and have avoided it. Done right you will also demonstrate an understanding of encapsulation and The Rule of Three. What is The Rule of Three? Read the link. It will save you from a lot of debugging.
  2. Test the smurph out of the hashtable class so you know you can trust it.
  3. Implement a free function that implements split USING the hashtable and NOT as a part of the hashtable. something like hashclass split(const std::string & str). You give it a string, it gives you a filled-out hashtable. The return by value is why the Rule of Three is very important to you. At this point it;'a also worth reading up on The Rule of Five and Copy Elision.
  4. Test the smurph out of split so that you know it works.
  5. Implement a free function that tests to see if two strings are replicas. This function, bool replica(const std::string & strA, const std::string & strB)
    1. Uses split on the two strings to get two hashtables.
    2. Then it tests which hashtable is the smaller. The smaller table can't possibly contain all of the words in the longer, but the longer may contain all of the smaller. If you already know which string should be found in the other, skip this step.
    3. Finally, iterate through the smaller hash table until you either find a word in smaller that isn't in the larger, return false, or you run out of words in the smaller table, return true.
  6. Test the replica tester function.

So to do all this your hashtable must be able to

  1. Take strings and insert them into the table,
  2. Allow the user to iterate the the contents, that is walk through the hash table element by element, and
  3. Allow the the user to query how many items are in the table.

If everything is nicely decoupled, you can test each piece separately making it much easier to pinpoint bugs.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Thank you for such a thorough response. Apologies for my noobness but could you show me how to "Implement a free function that implements split USING the hashtable and NOT as a part of the hashtable"? Still don't understand what you mean by this. Thanks again. – Joel Pou Mar 06 '17 at 22:20