#include <iostream>
using namespace std;
// programming
// gram
// 3
int substring(char *str, char* substr)
{
int lenstr=0, lensubstr=0;
for(int i=0;str[i]!='\0';i++)
{
lenstr++;
}
for(int i=0;substr[i]!='\0';i++)
{
lensubstr++;
}
int i=0,j=0;
while(i<lenstr)
{
if(str[i] == substr[j])
{
j++;
i++;
if(j == lensubstr)
{
return i-lensubstr+1;
}
}
else
{
i++;
j=0;
}
}
return -1;
}
int main() {
char str1[] = "programming";
char str2[] = "gram";
cout<<substring(str1,str2);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: