Program to search One String in Another String


void main()
{
char main_str[40],str[40];
int i,j,len1,len2;
clrscr();
printf("\n Enter the main String :");
gets(main_str);
printf("\n enter the String you wanna search : ");
gets(str);
len1=strlen(main_str);
len2=strlen(str);
i=0;
j=0;
while(str[i]!='\0')
{
while(main_str[j]!='\0')
{
if(str[i]==main_str[j])
{
i++;
if(len2==i)
{
j=j-len2+2;
printf("\n found at %d location",j);
break;
}

}//if
else
j++;

}//inner while
if(j==len1)
{
printf("\n not Found");
break;
}//if

}//outer while
getch();
}

0 comments: