Write a C program that reads a line of text and counts all occurrence of a particular word.
Program Code:
#include <stdio.h>
int main()
{
char str1[50], str2[50], str3[50], temp[50];
int len1, len2, len3, i, j, p, match, k,count=0;
printf("\n\n\t ENTER A SENTENCE:");
gets(str1);
printf("\n\n\t ENTER A STRING WHICH YOU WANT TO COUNT THE TIMES REPEATED: ");
gets(str2);
i=0; //Finding the length of sentence
while(str1[i]!='\0')
{
i++;
}
len1 = i-1;
i=0; //Finding the length of string, to delete
while(str2[i]!='\0')
{
i++;
}
len2 = i-1;
for(i=0;i<=len1;i++)
{
match = 1;
for(j=0;j<=len2;j++)
{
if(str2[j]!=str1[i+j])
{
match = 0;
break;
}
}
if(match)
{
count++;
}
}
printf("\t\t%s occurred %d times",str2,count);
}
Post A Comment:
0 comments: