Need help on finding word count
I need to find how many times a word is repeated in a sentence.
I'm almost done but there is a minor issue that I don't know how to fix.
If I input a sentence like "I LIke pIneapples and pIzzas" and search for "I", the output becomes 3 instead of the intended 1.
How do I fix this? The code I have is below.
Code:
#include <stdio.h>
#include "assignment.h"
#include <mem.h>void ex4() {
char sentence[1000], word[100];
int snum, wnum, frequency = 0, i, j;
puts("Enter Sentence");
gets(sentence);
puts("Enter word to find its frequency");
gets(word);
snum = strlen(sentence);
wnum = strlen(word);
for (i = 0; i < snum; i++)
{
if (sentence[i] == word[0])
{
for (j = 0; j < wnum; j++)
{
if (sentence[i + j] != word[j])
{
break;
}
}
if (j == wnum)
{
frequency++;
}
}
}
printf("The string '%s' occurs %d times \n", word, frequency);
}