Hello. Welcome back to the challenge 3 solution for section 19.
I'm in the section 19 workspace in the challenge_3_solution project.
And this is the project where we try to find the word matches
in the Romeo and Juliet play.
So let me go over my solution, and I wrote a simple function
to help me with the solution. And we'll talk about the function in just a second.
But let's just go through the solution at the beginning here. Here's my main
I've got my input file stream right here called infile.
Here's the word that I want to find. That's what the user is going to give us.
Here's the word that I've read. So each time I loop through that file,
I'm going to read a word. And then I've got the word count and match count,
which are just counters that we're going to increase when we see a word
and when we see a matching word.
Okay. So that's it. That's the preamble there.
And here's the good old recipe that we always adhere to, right.
I want to open the file Romeo and Juliet. If it's not open,
I display an error, and I leave.
So let's get to the part of the program that's doing all the work here.
So right here on line 29, I'm asking the user to enter the substring to search for,
suppose they enter love.
I'm reading that in from standard input.
And then I'm in my loop here. So while I want to read a word
from infile.
Okay. So I'm going to stop at any white space. I'm using the extraction operator here.
So I'm going to read a word into word read.
And if it didn't fail and if it didn't give me an end file,
I've got a word. So I'm going to increment the word count because I've seen a word.
And then I'm going to call a function,
which I'm going to show you in a minute that I wrote called find substring.
And the idea here is I send into this function
the word that I want to find and the word that was read from the file.
And if this word happens to be a substring in that word,
it's going to return true. When it returns true, I increment the match count.
If it doesn't return true, i don't increment the match count.
And I just simply go back up and loop again.
That's it. Pretty straightforward.
When I'm done with the loop, I've hit the end of file.
I simply display how many words were searched,
and how many times my matching word was found.
Okay. That's it. So let's talk about that find substring function now,
which I wrote up here.
And here it is. This function, as I said, expects
two parameters, two strings, and they're both const refs.
In this case, what's the word to find.
Okay. The word to find is what the user gave us love,
which is being passed into this. And what's the target.
The target is the word that we just read from the file,so maybe we read the word lovely
or we read the word Romeo
or whatever word we read.
So what we're going to do is we're going to use the find method
in the string class,
which says hey target, which is this guy
find that substring word to find within you
and give me back the index of where you found it.
I really don't care about the index. I just want to -- I just want to know if it was there or not.
So if it returns std string no position,
that means that it was not in the word, and I returned false. Otherwise, I returned true.
Like I said, I really don't care what position it's in. I just want to know whether
the substring exists in that target or not. That's it.
Really, really simple. Let's run this.
And let's search for -- again let's search for Romeo.
I said search for Juliet this time.
Juliet, 49 times we found the word Juliet.
All right. That's it. Now suppose we're trying to debug this along the way. We really want to see what
word was actually read from the file.
So what we could do here or even better let's just see the words that match.
We know the words that match, right, in this if statement right here, right. That's why we're incrementing match count
So what we could do is right in here we could stay std count.
And we could display the word that we just matched, right,
which was the word that we read. So we could display word read
followed by a space and no new lines that way it'll just display them one after the other.
So let's try running this again now.
And let's look for the word love.
There we go. Those are all the matches that we found.
Lovers, love, love with a question mark, punctuation,
love performing you can see here love song,
dear loved. There you go. So it gives us a pretty good
gauge as to the correctness of our program.
Obviously, we would have to do some real testing.
And absolutely, know ahead of time how many times this word exists.
Let's try it again with a capital l
would love four times.
Okay. So there you go.
And let's see how about Romeo.
You can see Romeo was found 132 times,
and it was found with a period at the end,
a period at the end again at the end of the sentence with question marks,
Romeo's with an apostrophe. So that's it.
That's the solution. If you came up with another solution,
if you figured out a way to do it by reading a whole line
of time and then splitting up the straight the words out of the line,
that's a lot more work. But if you did that you, think it's cool,
post it out there, so we can all learn from one another.
All right. So I hope you enjoyed this challenge.
In the next video, we'll learn about output streams and writing to files.