Contact Learn C
Copy

Program 95:To find whether the given substring is present in string or not

Program 95:
#include<stdio.h>
#include<string.h>
int search(char[],char[]);
main()
{
int i,j,len1,len2,k=0,flag=0;
char str[100],substr[100],temp[100]={0};
printf("Enter a string\n");
gets(str);
printf("Enter a sub string to be searched\n");
gets(substr);
len1=strlen(str);
len2=strlen(substr);
for(i=0;i<strlen(str);i++)
{
    k=0;
    for(j=i;j<i+len2;j++)
    {
        temp[k]=str[j];
        k++;
    }
    temp[k]='\0';
    flag=search(temp,substr);
    if(flag==1)
    {
        break;
    }
    else
    {
        continue;
    }
}
if(flag==1)
{
    printf("Search Successful\n");
}
else
{
    printf("Search Unsuccessful\n");
}
}

int search(char str1[],char substr1[])
{
    if(strcmp(str1,substr1)==0)
    {
        return(1);
    }
    else
    {
        return(0);
    }
}
Explanation:

This is same as program 91 Search substring

Output:

To find whether the given substring is present in string or not

No comments:

Post a Comment

Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide