Finding common words from two texts

Mar 28, 2018 at 6:52pm
Hi. I'm working on an assignment that asks me to find the common words from two texts(array of chars). I must do this using pointers. I have no ideea how to do this, as I am fairly new to pointers and C++. I completed several assignments using pointers already, but this just beats me. Any tips or ideas are very much apreciated.

Mar 28, 2018 at 7:58pm
Prints out the common words from the two texts. Uses pointers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Example program
#include <iostream>
#include <cstring>
#include <set>
#include <algorithm>

struct lex_compare {
    bool operator() (const char* lhs, const char* rhs) const {  
        return std::strcmp(lhs, rhs) < 0;
    }
};

int main()
{
    char text_A[512] = "On an exceptionally hot evening early in July a young man came out of the garret in which he lodged";
    char text_B[512] = "There was complete silence under the arcade except for the cooing of doves in the garden below";

    char* token;
    
    std::set<const char*, lex_compare> word_set_A;
    std::set<const char*, lex_compare> word_set_B;
    
    token = strtok(text_A, " ,.");
    while (token) {
        word_set_A.insert(token);
        token = strtok(nullptr, " ,.");
    }

    token = strtok(text_B, " ,.");
    while (token) {
        word_set_B.insert(token);
        token = strtok(nullptr, " ,.");
    }
    
    const char* common_words[512];

    auto it = std::set_intersection(word_set_A.begin(), word_set_A.end(),
                                    word_set_B.begin(), word_set_B.end(),
                                    common_words, lex_compare());
    
    int size = it - common_words;
    
    std::cout << "Common words: ";
    for (int i = 0; i < size; i++)
    {
        std::cout << common_words[i]  << " ";
    }
    std::cout << std::endl;
}


Last edited on Mar 28, 2018 at 7:59pm
Mar 28, 2018 at 8:09pm
You can use strtok to split the words for the first text and use strstr to see if they are in the second text.
Mar 28, 2018 at 8:19pm
True...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Example program
#include <iostream>
#include <cstring>

int main()
{
    char text_A[512] = "A   C D E F";
    char text_B[512] =   "B   D E";

    char* token = strtok(text_A, " ,.");
    while (token)
    {
        if (std::strstr(text_B, token))
        {
            std::cout << token << std::endl;   
        }

        token = strtok(nullptr, " ,.");
    }
}


Thanks for the tip, never used strstr before.
Last edited on Mar 28, 2018 at 8:23pm
Mar 28, 2018 at 8:36pm
Thank you! Works just fine. Is there any way to also check the frequency of words in each string using pointers?
Mar 28, 2018 at 8:47pm
Also, is it possible to do it using only pointers? (we can use strtok, but not sure about strstr). I think the main point of this was to compare the strings using pointers only.
Topic archived. No new replies allowed.