Hello people! Here is my WSQ12 in which I was told to create a program that could find a certain word (given by the user) in a text file (also given by the user).
So let’s start with the function. I created a function called find_word and its job was like the name, to find the word in the text file. There are two string variables: the word that the user is looking for and ‘sample’, that is the text file given by the user. The ‘ifstream myfile‘ allows you to look for the text file on your computer. Then I put an if statement that says ‘if the file is open do this…’. As you want to find a word as many times as it appears in the text file, you must analyze each line of the text file, here is where the getline helps you. This function goes by every line and then with the comparison, you can find the word and the counter increases.
Then you have to close the file with myfile.close()
And here is the rest of the program, where I ask for the word and the name of the text file. Then I just called my function and that’s it. The result was in the function find_word.
You can look at my code here in GitHub. Feel free to comment.
Thank you Ken for the help with this program!!
·Word Count· #WSQ12 by alexhfoo is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.