Here is a C++ to check whether a sub string is present in a given string
SOURCE CODE OUTPUT
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str[30],subst[20];
int i,j=0;
cout<<"Enter String : ";
cin>>str;
cout<<"Enter sub string : ";
cin>>subst;
for(i=0;str[i]!='\0';i++)
{
if(str[i]==subst[j])
j++;
else if(str[i]!=subst[j])
j=0;
if(subst[j]=='\0')
{
cout<<"Sub string is present";
return 0;
}
}
cout<<"Substring is not present ";
return 0;
}
#include<string.h>
using namespace std;
int main()
{
char str[30],subst[20];
int i,j=0;
cout<<"Enter String : ";
cin>>str;
cout<<"Enter sub string : ";
cin>>subst;
for(i=0;str[i]!='\0';i++)
{
if(str[i]==subst[j])
j++;
else if(str[i]!=subst[j])
j=0;
if(subst[j]=='\0')
{
cout<<"Sub string is present";
return 0;
}
}
cout<<"Substring is not present ";
return 0;
}
Any questions regarding to program please write in comments.
No comments:
Post a Comment