I'm trying to write a program that will take in several lines of text and then take in a target word to look for in the lines of text. I need to return how many times the target word appears in the lines of text. I have everything but the function to find the number of occurrences of the target word. I've been working on it for a while and can't find a way to do it.
Code:
#include <stdio.h>
#include <string.h>
int analyzeLine(char text[], char target[]);
// Function for analyzing lines of text
int main(void)
{
int i = 0; // Variable for progressing through a for loop
int lines; // Number of lines to be analyzed
int occurrences = 0;
int sum = 0;
char repeat = 'y'; // Used to prompt if the program should be repeated
char text[81]; // String for lines of text
char target[100];
while(repeat == 'y')
{
printf("Please enter the target word: ");
fgets(target, sizeof(target), stdin);
printf("Please enter the number of lines to be analyzed: ");
scanf("%d", &lines);
fflush(stdin);
// Input and analyzation of the lines of text
for(i=0; i<lines; i++)
{
printf("\n\nLine #%d:\n", i+1);
fgets(text, sizeof(text), stdin);
sum = analyzeLine(text, target);
occurrences += sum;
}
printf("\n\n");
// Printing the number of occurrences to the screen
printf("The target occurs %d times\n", occurrences);
// Prompt for repeating of program
printf("\nWould you like to analyze more lines?");
printf("\nEnter Y for yes or N for no: ");
scanf(" %c", &repeat);
repeat = tolower(repeat);
fflush(stdin);
}
system("pause");
return 0;
}
int analyzeLine(char text[], char target[])
{