Hi, the following is the code for returning the position of a substring within a string(Brute force algorithm)
//Brute Force Algorithm
#include<stdio.h>
#include<string.h>
int main(void)
{
char s1[20],s2[10];
int x1,x2,j,k,i;
printf("Enter string 1:");
gets(s1);
printf("Enter string 2:");
gets(s2);
x1=strlen(s1);
x2=strlen(s2);
if(x1<x2)
return 0;
for(i=0;i<=x1-x2;i++)
{
j=i;
k=0;
while((s1[j]==s2[k])&&k<x2)
{
j++;
k++;
}
if(k==x2)
printf("%d",i);
break;
}
return 0;
}
When I run the program, it does not print the position of the substring. What is the error? Help!!