C program to check if the string is the substring of another string

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main() 
{
     char array1[20];
     char array2[20];
     int i=0,j=0, l, flag=0;
        printf("Enter the first string");
     gets(array1) ;
       printf("\nEnter another string");
       gets(array2) ;
      l=strlen(array2);
      while(array1[i]!='\0')
      {
          if(array1[i]==array2[j])
            {
                if(j==l-1)
                { 
                  printf("yes");
                  exit(0);
                  flag=1;
                }
                 else
                 j++;
              
            }
            else
            j=0;
            i++;
      }
      if(flag==0) 
      printf("no");
      return 0;
}
    

Output:
 Enter the first string
 all the best
 Enter another string
 the
 yes

Explanation:
The C program is to determine whether the second string is the substring of the first string. 
If you want to know what is string click on the below link,

What is substring? 
If a string name of one is present in some portion of another string, then that string is considered as the substring of another string. 
Example:
1. char string1[20]="custardapple";
    char string2[20]="star";
     string2 is "star" Which is present in some portion of string1 "custardapple".
Hence, string1 is the substring of string2. 

2.  char string1[20]="banana";
    char string2[20]="and";
    Strong is "and" Which is not present in any portion of string1 "banana".
Hence, string2 is not a substring of string1. 

Explanation of code:

We have declared two character arrays, array1 and array2. 

We took the runtime input of both the array. 

Instead of scanf, we use the gets() function. Why? 
scanf does not count the spaces in the string. If we assigned any space, it will be considered as a NULL character. 
gets() function can count the spaces in the string, and also it benefits us to assign the sentence to the string. 

Consider
string1 is given input as, 
"all the best"
string2 as, 
"the"
Now, we have to find whether string2 is a substring of string1 or not. 

Variable l consists of the length of string2. 
l=strlen(string1); // this is the string function to find the length of the string. 
If you want to know about string functions, click on the below link, 

We have to compare the elements of string1 with string2. 

By default, the last element of a string consists of the NULL character. 

A while loop is used to iterate all the elements in the string1. 

If-statement is used to compare the element of string1 and string2. 

If the element of string1 is equal to the element of string2, then the next element of both the strings has to be compared. 
Otherwise, string2 has to be in the 0th position, and string1 has to be incremented. 

The length of string2 is 3. If continuous 3 elements are matched, then string2 is a substring of string1. 
Hence, a nested if is used to check if, 
  i==l-1  // "i" will be from 0 to 2 position. hence, the value of l should be l-1. 
A flag is set to 0. When we get a substring flag will be 1. otherwise, it's 0.
Hence, an else is used for first if, when the flag remains 0 that means, string1 is not sub-string of string 2.
If string2 is a substring of string1, "yes' will get print. Otherwise, the flag will remain 0, and no will get print. 

This was the logic for finding the substring. 
I hope you like the logic and the explanation.