counting a word in a file

Hi ! I need help here , my program is reading a text file and finding how many the word "The" in it ,but our instructor want it to be counted even if there is no space before it or after it , and i wrote this program but it only read the word "The" if before it and after it a space , so I'll be thankful if someone helps me with it =).

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

using namespace std;

int main()
{
	char input[100];
	string str;
	int counter = 0;

	cout << "Enter input file name: ";
	cin >> input;
	strcat(input, ".txt");
	ifstream fin(input);
	fin >> str;
	while(str.length() > 0)
	{
		if(str == "The")
			counter++;
		fin >> str;
	}
	cout << "Counter = " << counter << endl;

	return 0;
}
You mean, tah the words like "thesaurus" should increment it too?
yes
1
2
3
4
5
6
7
8
9
while(fin >> str) {
    for(auto& ch: str)
        ch = std::tolower(ch); //handles different case situations
    int x = 0
    while( (x = str.find("the", x)) != std::string::npos) {
        x += 3; //Skip already found sequence
        ++counter;
    }
}
Last edited on
closed account (3qX21hU5)
Here is some code to help you. Makes sure you know exactly what it is doing and why it is doing it before you use it!!! Also there is something wrong with this code that you will need to fix that I added in there on purpose. It also isn't complete I left out the hard part for you to figure out :)

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

using namespace std;

int main()
{
    ifstream file("YourFileName.txt");

`   // Vector will holds each word of the file. String will be the temp holder
    // for each word.
    vector<string> words;
    string word;
    int counter = 0;

    // Reads each word from the file and converts it to lowercase
    // then adds that word into the vector.
    while (file >> word)
    {
        for (auto i = 0; i != word.size(); ++i)
            word[i] = tolower(word[i]);

        words.push_back(word);
    }

    // Goes through each element in the vector and searches that word
    // for "the" if it finds it it will add 1 to the counter.
    for (auto i = 0; i != words.size(); ++i)
    {
        unsigned found = words[i].find("the");
        if (found != string::npos)
            ++counter;
    }
    
    // Prints how many it found
    cout << counter << endl;
}


Hope this helps and wish you the best of luck, let us know if you have any questions.

Also MiiNiPaa's approach is much better since in his example you don't need to store anything you just go through the file word by word and examine each word for "the". I just figured storing the words might show how this works a little better.
Last edited on
Thank you guys I appreciate your help

I'm working on it =)
Topic archived. No new replies allowed.