A program to check if 2nd string is a substring of first string. It displays “Yes” for all cases, Moreover, I get a runtime error whenever i give the second string as ‘q**q’, why is it so..? * stand for any letter.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str1[20], str2[20];
int l1, l2, i1, i2=0;
gets(str1);
gets(str2);
l1=strlen(str1);
l2=strlen(str2);
if(l1>=l2)
{
for(i1=0; i1<=l1-l2+i2, i2<l2; i1++)
{
if(str2[i2]==str1[i1])
i2++;
}
if(i2==l2)
printf("Yes");
else
printf("No");
}
else
printf("No");
getch();
return 0;
}
The condition in the for statement
really doesn’t make sense. The code
i1<=l1-l2+i2, i2<l2evaluates the part before the comma, discards the result, and then evaluates the part after the comma.Perhaps you intend to write
i1<=l1-l2+i2 && i2<l2.