Wednesday, 26 August 2015

Write a C Program To FInd that will read a text and count all occurrences of a particular word.

Write a C Program To FInd that will read a text and count all occurrences of a particular word.
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int main()  
4:  {  
5:        char str[100],find[100];  
6:        int i,j,cnt=0,flag;  
7:        clrscr();  
8:        printf("Enter the string :");  
9:        gets(str);  
10:        printf("Enter the srting that you want to find : ");  
11:        gets(find);  
12:        for(i=0;str[i];i++)  
13:        {  
14:              flag=0;  
15:              if(str[i]==find[0])  
16:              {  
17:                    flag=1;  
18:                    for(j=1;find[j];j++)  
19:                    {  
20:                          if(str[i+j]!=find[j])  
21:                          {  
22:                                flag=0;  
23:                                break;  
24:                          }  
25:                    }  
26:                    if(flag==1)  
27:                          cnt++;  
28:              }  
29:              else  
30:              {  
31:              }  
32:        }  
33:        printf("%s occurs in %s %d times\n",find,str,cnt);  
34:        getch();  
35:        return 0;  
36:  }  

No comments:

Post a Comment