Count how many times a banned word has occured from a text file

I have been having issues trying to get a map, vector etc to work in order to print out how many times one of the banned words has occured in the text file.

Initially, the banned.txt file is read in order to store what files are banned in an array. Then it is to compare the two .txt files in order to ensure that the banned words are able to be filtered through the text and found.

Any help on getting it to find all of the words from the text that are in banned.txt?

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
71
72
73
74
75
76
77
78
79
80
81
 #include <iostream>
#include <string>
#include <fstream>
using namespace std;





int main()
{
	//Step 1 read in 
	ifstream infile("banned.txt"); //The list of search words is in "search1.txt".
	string words[8]; //array to store the list of search words (words we need to find)
	int wordCount = 0;
	
	
	if (!infile) //checks we can open the file - validation example
	{
		cout << "ERROR: ";
		cout << "Can't open search1.txt\n";
	}
	//before we close this file we want to read the contents into an array, we can do this with a for loop
	for (int i = 0; i < 8; ++i)
	{
		infile >> words[i]; //by using the operator >> it reads from the variable "infile" and stores it into the array called words.
	}
	//to check we have stored the words we are searching for correctly inside the array, we can then use a cout to output them
	for (int i = 0; i < 8; ++i)
	{
		cout << words[i] << " "; //output each member of the array for debugging purposes.
	}
	cout << endl;
	infile.close(); //we have now finished with this file and all the information from the file has been stored into the array called words
	//the next step is to read in the line of characters in that we will use to search for specific words that are stored inside the array called words
	string text; //here we have declared a string called text which we can store our line of text in.                                              
	infile.open("text1.txt");
	getline(infile, text);
	while (!infile.eof())
	{
		cout << text;
		cout << endl;
		getline(infile, text);
	}
	infile.close();
	//Step 2
	//Do a comparison between the words from "search1.txt" and the words from "text1.txt". 
	//You need to find the occurrence of each word from “search1.txt” in the string of characters from “text1.txt”. 
	//Output the word, whether it has been found and, if found, the index of its location in the array,
		
	
	for (int i = 0; i < 8; ++i)
	{
		int position = text.find(words[i]); //uses the inbuilt function called find()
		

		cout << "\"" << words[i] << "\", ";
		if (position != string::npos)
		{
			
			cout << "Found, location " << position << endl;
			

			
			
			
			
		}
		else
		{
			cout << "Not Found" << endl;
		}

	}


	
	

	system("pause");
}



Here are the text files components too.

banned.txt
1
2
3
4
5
6
7
8
cat
dog
aim
add
ear
back
punk
able



text1.txt
1
2
3
4
5
6
7
this is a bit of doggoral but it will allow me to doggedly persue my aim of cataloguing the
 effect of applying a word filter the filter should pick out
 words such as cat dog and aim 
Who knows what other pernicious words it will endeavour to protect the innocent
 from there is no punctuation in order to 
make it easier to identify the words and i h
ave also written it entirely in lower case you will feel like a dog if you do not manage the aim of this assignment but i expect everyone to be top cat

[/center]
Last edited on
Woah! I'd suggest you edit your post to remove those [center] tags. Nobody wants to read code like that.
First, you already have a thread on this, don't you? http://www.cplusplus.com/forum/beginner/277592/
Do not double-post.


Wow. Do you find that indentation easy to read?
You will have to sort out case, multiple occurrences etc.

You will have to sort out what you do with the words once you've read them in - your two posts seem to differ in that respect.


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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

vector<string> readFile( const string &filename )
{
   vector<string> result;
   ifstream infile( filename );
   if ( !infile )
   {
      cout << "ERROR reading " << filename << '\n';
   }
   else
   {
      for ( string s; infile >> s; ) result.push_back( s );
   }
   return result;
}


int main()
{
   vector<string> banned = readFile( "banned.txt" );
   vector<string> text   = readFile( "text1.txt"  );
   for ( const string &b : banned )
   {
      auto f = find( text.begin(), text.end(), b );
      if ( f == text.end() ) cout << b << " is not found\n";
      else                   cout << b << " is found at word " << f - text.begin() + 1 << '\n';
   }
}

cat is found at word 34
dog is found at word 35
aim is found at word 16
add is not found
ear is not found
back is not found
punk is not found
able is not found
Last edited on
//Step 2
//Do a comparison between the words from "search1.txt" and the words from "text1.txt".
//You need to find the occurrence of each word from “search1.txt” in the string of characters from “text1.txt”.
//Output the word, whether it has been found and, if found, the index of its location in the array,


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 <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

vector<string> readFile( const string &filename )
{
   vector<string> result;
   ifstream infile( filename );
   if ( !infile )
   {
      cout << "ERROR reading " << filename << '\n';
   }
   else
   {
      for ( string s; infile >> s; ) result.push_back( s );
   }
   return result;
}


int main()
{
   vector<string> banned = readFile( "banned.txt" );
   vector<string> text   = readFile( "text1.txt"  );

   for ( const string &b : banned )
   {
      auto f = find( text.begin(), text.end(), b );
      if ( f == text.end() )
      {
         cout << b << " is not found\n";
      }
      else
      {
         vector<int> loc;
         while( f != text.end() )
         {
            loc.push_back( f - text.begin() + 1 );
            f = find( f + 1, text.end(), b );
         }
         cout << b << " is found " << loc.size() << " time(s), at words ";
         for ( int i : loc ) cout << i << ' ';
         cout << '\n';
      }
   }
}


cat is found 2 time(s), at words 34 99 
dog is found 2 time(s), at words 35 81 
aim is found 3 time(s), at words 16 37 88 
add is not found
ear is not found
back is not found
punk is not found
able is not found

Topic archived. No new replies allowed.