Binary search

Program to conduct binary search on an enteres STRING...


#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int n,l,h,min,flag=0,pos;
char a[10],ch;
clrscr();
printf("\nEnter the sorted string : ");gets(a);
n=strlen(a);
printf("\nEnter the character to be searched : ");
scanf("%c",&ch);
l=0;
h=n-1;
while(l<=h)
{
min=(l+h)/2;
if(a[min]==ch)
{
flag=1;
pos=min;
break;
}
else if(a[min]>ch)
h=min-1;
else
l=min+1;
}
if(flag)
printf("\nThe reqd character found in the string @ position!!",pos+1);
else
printf("\nChracter not found...! search unsuccessful");
getch();
return 0;
}

Category:

0 comments: