Following function finds whether a given string is a substring of another string. If a given string is a substring of another string it returns 1 which means success, otherwise it will return 0.
int substr(char* str1, char* str2)
{
int m, n, i, j;
m=n=0;
//Find the lengths of strings
while(str1[m])
++m;
while(str2[n])
++n;
for(i=0; i<m-n+1;++i)
{
for(j=0; j<n;++j)
if(str2[j] != str1[i+j])
break;
if( j == n )
return 1; //Substring exists
}
return 0; //Substring does not exists
}
int substr(char* str1, char* str2)
{
int m, n, i, j;
m=n=0;
//Find the lengths of strings
while(str1[m])
++m;
while(str2[n])
++n;
for(i=0; i<m-n+1;++i)
{
for(j=0; j<n;++j)
if(str2[j] != str1[i+j])
break;
if( j == n )
return 1; //Substring exists
}
return 0; //Substring does not exists
}
No comments:
Post a Comment