Heloo

I have this problem

txt1
Dad has apples
Mom has pears
Grandma has grapes


txt2
apples
pears

output
Dad has apples
Mom has pears



help pls
Last edited on
Do you have a question, or problem with your code?

I can not take the whole sentence
a sentence with a word that is repeated in txt2


txt1
Dad has apples
Mom has pears
Grandma has grapes


txt2
apples
pears

output
Dad has apples
Mom has pears

Last edited on
?
enybady
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
#include <iostream>
#include <sstream>
#include <unordered_set>
#include <string>

// assumes that a word is any sequence of non-white-space characters
// return set of words read from the input stream
std::unordered_set<std::string> words_read_from( std::istream& stm )
{
    std::unordered_set<std::string> words ;
    std::string str ;
    while( stm >> str ) words.insert(str) ;
    return words ;
}

// assumes that a word is any sequence of non-white-space characters
// return set of words in phrase
std::unordered_set<std::string> words_in( const std::string& phrase )
{
    std::istringstream stm(phrase) ; // create an input string stream to read from
    return words_read_from(stm) ;
}

// return true if 'phrase' contains any of the words in the set 'words'
// note: case-sensitive
bool contains_word_in( const std::string& phrase, const std::unordered_set<std::string>& words )
{
    for( const std::string& str : words_in(phrase) ) // for each unique word in 'phrase'
    {
        if( words.find(str) != words.end() ) // if it is present in the set 'words'
            return true ;
    }
    return false ;
}

int main()
{
    std::istringstream txt1( "Dad has apples\n"
                             "Mom has pears\n"
                             "Grandma has grapes\n"
                             "Grandpa has nectarines\n"
                             "I have oranges\n" ) ;

    std::istringstream txt2( "oranges\n"
                             "apples\n"
                             "pears\n" ) ;

    // step 1: get the set of all words in txt2
    const auto words_in_txt2 = words_read_from(txt2) ;

    // step 2: for each line in txt1
    std::string line ;
    while( std::getline( txt1, line ) )
    {
        // step 2a. check if the line contains any word in the set of words in txt2
        if( contains_word_in( line, words_in_txt2) ) // if it does
            std::cout << line << '\n' ; // print the line
    }
}

http://coliru.stacked-crooked.com/a/ad50642ac266f995
thanks JLBorges

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
int main()
{
	ifstream txt1;
	ifstream txt2;
	ofstream output;
	txt1.open("marius.txt");
	txt2.open("marius1.txt");
	output.open("marius2.txt");


		// step 1: get the set of all words in txt2
		const auto words_in_txt2 = words_read_from(txt2);

	// step 2: for each line in txt1
	string line;
	while (getline(txt1, line))
	{
		// step 2a. check if the line contains any word in the set of words in txt2
		
		if (contains_word_in(line, words_in_txt2)) // if it does

			output << line << endl;// print the line
	
		
	}

	output.close;
	txt1.close;
	txt2.close;
	
	system("pause");
	return 0;
}


I want to transfer the answer in output file
> I have a problem with the code
> I want to change
txt1
Momhaspears
Grandmahasgrapes
Grandpahasnectarines
Ihaveoranges

txt2
pear
grap

output
Momhaspears
Grandmahasgrapes


1. Make a genuine attempt to understand the earlier version of the program.
2. Then, make a genuine attempt to modify it to get the results that you want.
3. After you have completed steps 1. and 2., you may want to have a look at this code.

You do learn a lot by studying code written by others (though teachers who believe that the primary goal of formal education is evaluation rather than acquisition of knowledge tend to react violently to this premise). But you will internalise that knowledge only when you apply it in your own code.

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

// assumes that a word is any sequence of non-white-space characters
// return set of words read from the input stream
std::unordered_set<std::string> words_read_from(std::istream& stm)
{
    std::unordered_set<std::string> words;
	std::string str;
	while (stm >> str) words.insert(str);
	return words;
}

// return true if 'phrase' contains any substring in the set 'substrings'
// note: case-sensitive
bool contains_substring_in(const std::string& phrase, const std::unordered_set<std::string>& substrings)
{
	for (const std::string& str : substrings ) // for each substring 'str' in 'substrings'
	{
		if ( phrase.find(str) != std::string::npos ) // if 'phrase' contains the substring
			return true;
	}
	return false;
}

int main()
{
	std::istringstream txt1("Dadhasapples\n"
		"Momhaspears\n"
		"Grandmahasgrapes\n"
		"Grandpahasnectarines\n"
		"Ihaveoranges\n");

	std::istringstream txt2("pear\ngrap\n" ) ;

	// step 1: get the set of all words in txt2
	const auto words_in_txt2 = words_read_from(txt2);

	// step 2: for each line in txt1
	std::string line;
	while (std::getline(txt1, line))
	{
		// step 2a. check if the line contains any substring from the set of words in txt2
		if ( contains_substring_in( line, words_in_txt2 ) ) // if it does
			std::cout << line << '\n'; // print the line
	}
}

http://coliru.stacked-crooked.com/a/b3da3f91c17783c8
thanks





Last edited on
Topic archived. No new replies allowed.