Write a C++ function that meets the following specification. Name: wordFinder Inputs: 1) input filename (string) 2) pattern to find (string) Output: An integer representing the number of lines that contain the provided pattern Purpose: The goal of this function is to open the file and read it line by line. In each line, you want to see if the pattern exists. If it does, then a counter should be incremented. If the file is unable to be open for any reason, a -1 should be returned. Otherwise, the number of lines containing the pattern should be returned. Assuming line is a string variable that holds the current line and pattern is a string variable holding the pattern to check for, the following code will determine if the pattern is in a string holding the current line: if (line.find(pattern) != string::npos) //Then the pattern is in the line else //The pattern is not in the line
Write a C++ function that meets the following specification.
Name: wordFinder
Inputs: 1) input filename (string)
2) pattern to find (string)
Output: An integer representing the number of lines that contain the provided pattern
Purpose: The goal of this function is to open the file and read it line by line. In each line, you want to see if the pattern exists. If it does, then a counter should be incremented. If the file is unable to be open for any reason, a -1 should be returned. Otherwise, the number of lines containing the pattern should be returned. Assuming line is a string variable that holds the current line and pattern is a string variable holding the pattern to check for, the following code will determine if the pattern is in a string holding the current line:
if (line.find(pattern) != string::npos)
//Then the pattern is in the line
else
//The pattern is not in the line
Step by step
Solved in 4 steps with 5 images