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.
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++;
}
elseif (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!
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...
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.
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:
constchar* StrStr(constchar* src, constchar* pattern)
{
//src1 points to start of src string
//src2 points to end of src string
constchar* src1 = src;
constchar* src2 = src + strlen(src);
//pat1 points to start of pattern string
//pat2 points to end of pattern string
constchar* pat1 = pattern;
constchar* pat2 = pattern + strlen(pattern);
//initialize return pointer
constchar* 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
elseif (pat1 == pat2)
{
substringPtr = src1 - strlen(pattern);
return substringPtr;
}
//if no match is found, adjust src and pat pointers
elseif (*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.