Counting number of occurences of a word in a text file

I've been struggling with this for a while, I have to write a function that accepts a word (in my case the word is "the") and file pointer, and counts the number of times the given word appears (case insensitive) within the file associated with the pointer. This means the two words "the" and "THE" should both be counted.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int WordCount :: countWords(string wrd)
{
	int counter=0;
	string temp = "";
	while (getline(*file,temp))
	{  
	  for (int i = 0; i < temp.length();i++)
	  { 
	    if (tolower(temp[i] + temp[i+1] + temp[i+2]) == (wrd[0] + wrd[1] + wrd[2]))
	    {
		  counter++;
	    }
	    else if (toupper(temp[i] + temp[i+1] + temp[i+2]) == toupper(wrd[0] + wrd[1] + wrd[2]))
	    {
		  counter++; 
	    }
	  }
	}
	
	return counter;	
}


This is what I have come up with, but I get an incorrect value. It was suggested to us to consider using the strcpy() or strstr() functions, but I don't know how to use them.

I know there has to be a more effective way, any help would be greatly appreciated!
Last edited on
closed account (10X9216C)
How do you think adding all the letters in a word means its the same word?

"aab" == "baa" // this is true for your equation

Not only that, using tolower() on the addition is meaningless. Read documentation and don't assume it does magic. tolower/upper only changes one character to it's equivalent lower/upper char. Adding 3 letters together probably doesn't create a valid letter anymore, just some symbol for the integer representation of whatever it is.

There isn't anything to fix here other than scrap it and try again...

http://www.cplusplus.com/reference/string/string/find/
Last edited on
Actual help would be appreciated, thank you.
closed account (10X9216C)
It is actual help. You didn't even try to make something that works. That or you have a mistaken idea of what a word is. I told you what was wrong with your implementation. I even linked to you a class with a function you can use instead of whatever monstrosity you've invented.

The fact you said
It was suggested to us to consider using the strcpy() or strstr() functions, but I don't know how to use them.
makes it seem to be an assignment. Also the fact he is suggesting you use the C standard lib instead of C++ while seemingly using C++ i don't think you are getting your money's worth.

Seems like you've already posted this same problem 3-4 times also.
Last edited on
Coincidentally, I was working on a problem that matched quite closely to you problem statement. I added comments to it and am posting the solution. It needs a slight modification to get to your solution. Hopefully you can get the usages and modify it. Here you go:

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
const char* StrStr(const char* src, const char* pattern)
{
	//src1 points to start of src string
	//src2 points to end of src string
	const char* src1 = src; 
	const char* src2 = src + strlen(src);
	
	//pat1 points to start of pattern string
	//pat2 points to end of pattern string
	const char* pat1 = pattern; 
	const char* pat2 = pattern + strlen(pattern);

	//initialize return pointer
	const char* substringPtr = NULL;
	int AdjustSrcPtr = 0;

	while (src1 != src2)
	{
		//if character match is found, move on to next character
		if (pat1 != pat2 && *src1 == *pat1)
		{
			++src1;
			++pat1;
		}
		//if pattern string ended, means substring match is found
		//adjust src pointer and return
		else if (pat1 == pat2)
		{
			substringPtr = src1 - strlen(pattern);
			return substringPtr;
		}
		//if no match is found, adjust src and pat pointers
		else if (*src1 != *pat1)
		{
			AdjustSrcPtr = pat1 - pattern;
			src1 = src1 - AdjustSrcPtr + 1;
			pat1 = pattern;
		}
	}

	//if we reach here, substring match was not found
	return NULL;
}


Basically this will need to be modified at condition pat1 == pat2. Instead of returning, you'd count number of occurrances of the pattern and return only when the src string ends. In my case I have a return statement when pattern string ends too.
Last edited on
Thank you very much n4nature, that gives me something to work with
Last edited on
Topic archived. No new replies allowed.