Monday, 28 July 2014

C algo to search a word in sentence without string function and pointers method 2



Search a word in sentence or phrase without string function and pointers and indicate its position (Method 2)


No need to explain 

Logic


Code



#include<stdio.h>
#include<conio.h>

void main(void)
{
clrscr();

int a[100];  // to store string or phrase
int b[20];   // to store word or letters to search
int i=0,j=0;

printf("\n Enter sentence or phrase \n");
while((a[i++]=getche())!='\r');
i--;
printf("\n \n Enter word or letters to search for \n");
while((b[j++]=getche())!='\r');
j--;

int r;                 // check the match in sequence..
int match=0;  // indicate that match or not.

for(int x=0; x<i; x++)
{
 r=0;
 for(int y=0; y<j; y++)
 { if(b[y]==a[x+y])
   r++;
 }

 if(r==j)
  {
   printf("\n \n Matched at %d",x+1);
   match=1;
  }
}
if(match==0)
printf("\n Match not found");


getch();
}




Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: TechyBuz

0 comments:

Post a Comment