Thursday, 27 August 2015

Write a C Program that finds a given word in a string.

Write a C Program that finds a given word in a string.
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int main()  
4:  {  
5:        char str[100],find[100];  
6:        int i,j,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:              if(str[i]==find[0])  
15:              {  
16:                    flag=1;  
17:                    for(j=1;find[j];j++)  
18:                    {  
19:                          if(str[i+j]!=find[j])  
20:                          {  
21:                                flag=0;  
22:                                break;  
23:                          }  
24:                    }  
25:                    if(flag==1)  
26:                          break;  
27:              }  
28:        }  
29:        if(flag==1)  
30:              printf("%s is found in %s at %d position",find,str,i+1);  
31:        else  
32:              printf("%s is not found in %s",find,str);  
33:        getch();  
34:        return 0;  
35:  }  

No comments:

Post a Comment