I wrote this code to find if string s2 is a substring of string s1. However I'm getting time limit exceeded. How can I make my algorithm better?
#include<stdio.h>
int main()
{
char s1[20], s2[20];
scanf("%s %s", s1,s2);
char *p, *q, *count;
p=s1;
q=s2;
int i=21;
do{
if(*p==*q) /* the strings are compared */
{
count=p;
while(*p==*q)
{(*p)++; (*q)++;}
if(*q=='\0')
{
i=0;
while(&s1[i]!= count) /* count =p to get the index where the substring match starts */
i++;
printf("%d",i);
break;
}
else
{ q=s2; (*p)--;}
}
}
while((++(*p))!='\0');
if(i==21) /* if s2 is not a substring print -1 */
printf("-1");
return 0;
}