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);
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: