Comparing words between two files/strings

Hi all! So assuming that i'm only able to use std lib functions and strings, can anyone give any insight on how I would loop through each word of a sentence/string? I know that ideally i'd want to pull out each word and do what I need to do to it, and then continue onto the next one, to compare it to another string and see if any words match. Though i'm not sure where to proceed from here:

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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    string sentence;
    string words;
    ifstream sentenceFileObj;
    ifstream wordsFileObj;

    sentenceFileObj.open("sentence.txt");
    cout << "Here is your sentence.txt file:" << "\n \n \n \n \n";

    while(getline(sentenceFileObj, sentence)){
        //print text of file: store it in a string.
        cout << sentence;
    }
    sentenceFileObj.close();
    cout << "\n \n \n \n";

    // opening new instance of the file:
    sentenceFileObj.open("sentence.txt");
    cout << "These are the individual sentences, one on each line:" << endl;

    // printing each sentence out, storing in a string
    string individualSentence;
    while (getline(sentenceFileObj, individualSentence, '.')){
            cout << individualSentence  << endl;
    };

    sentenceFileObj.close();
    cout << "\n \n \n \n";

    // printing out words file.
    wordsFileObj.open("words.txt");
    cout << "Here is your words.txt file:" << "\n \n \n \n \n";

    while (getline(wordsFileObj, words)){
        cout << words << endl;
    }
    wordsFileObj.close();
    cout << "\n \n \n \n \n";

    
    
}



Last edited on
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
#include <string>
#include <cctype>

// return next word in the string after position pos
// (or an empty string if there are no more words)
// update pos to the position just after the last character in word
std::string next_word( const std::string& sentence, std::size_t& pos )
{
    // skip over leading non-alpha characters
    while( pos < sentence.size() && !isalpha( sentence[pos] ) ) ++pos ;

    std::string word ;
    // add consecutive alpha characters one by one to the word
    for( ; pos < sentence.size() && isalpha( sentence[pos] ) ; ++pos ) word += sentence[pos] ;

    return word ;
}

// return true if sentence contains word
// note: case sensitive, assumes word does not contain non-alpha characters
// TO DO: consider making the comparison case-insensitive
bool contains_word( const std::string& sentence, const std::string& word )
{
    std::size_t pos = 0 ;
    while( pos < sentence.size() )
        if( next_word( sentence, pos ) == word ) return true ;

    return false ;
}
Forgive me if i'm way off somewhere, but i'm getting a function-definition error for the code thus far ( I should note that I do not need to worry about capitals or lower cases, so long as the words match):

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    string sentence;
    string words;
    ifstream sentenceFileObj;
    ifstream wordsFileObj;

    sentenceFileObj.open("sentence.txt");
    cout << "Here is your sentence.txt file:" << "\n \n \n \n \n";

    while(getline(sentenceFileObj, sentence)){
        //print text of file: store it in a string.
        cout << sentence;
    }
    sentenceFileObj.close();
    cout << "\n \n \n \n";

    // opening new instance of the file:
    sentenceFileObj.open("sentence.txt");
    cout << "These are the individual sentences, one on each line:" << endl;

    // printing each sentence out, storing in a string
    string individualSentence;
    while (getline(sentenceFileObj, individualSentence, '.')){
            cout << individualSentence  << endl;
    };

    sentenceFileObj.close();
    cout << "\n \n \n \n";

    // printing out words file.
    wordsFileObj.open("words.txt");
    cout << "Here is your words.txt file:" << "\n \n \n \n \n";

    while (getline(wordsFileObj, words)){
        cout << words << endl;
    }
    wordsFileObj.close();
    cout << "\n \n \n \n \n";



    string next_word(const string& sentence, size_t& pos){
      while (pos < sentence.size() && !isalpha( sentence[pos])) ++ pos;

        string word;
        for (pos = 0; pos < sentence.size() && isalpha( sentence[pos]) ; ++pos) word +- sentence[pos];

        return word;

    bool contains_word( const string& sentence, const string% word){
    size_t pos = 0;
      while ( pos < sentence.size() )
        if (next_word ( sentence, pos ) == word){
         return true;

        }else{

         return false;
        }
    }

 }
Last edited on
Define the functions outside main(), and then call contains_word from main(). For example:

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
#include <iostream>
#include <string>
#include <cctype>

// return next word in the string after position pos
// (or an empty string if there are no more words)
// update pos to the position just after the last char in word
std::string next_word( const std::string& sentence, std::size_t& pos )
{
    // skip over leading non-alpha characters
    while( pos < sentence.size() && !isalpha( sentence[pos] ) ) ++pos ;

    std::string word ;
    // add consecutive alpha characters one by one to the word
    for( ; pos < sentence.size() && isalpha( sentence[pos] ) ; ++pos ) word += sentence[pos] ;

    return word ;
}

// return true if sentence contains word
// note: case sensitive, assumes word does not contain non-alpha characters
// TO DO: consider making the comparision case-insensitive
bool contains_word( const std::string& sentence, const std::string& word )
{
    std::size_t pos = 0 ;
    while( pos < sentence.size() )
        if( next_word( sentence, pos ) == word ) return true ;

    return false ;
}

int main()
{
    const std::string sentence = "how now brown cow!" ;
    const std::string word = "brown" ;

    if( contains_word( sentence, word ) ) std::cout << "found the word in sentence\n" ;
}
Last edited on
Topic archived. No new replies allowed.