TechoSagar: Naive Pattern Searching using C++

Search

Google Alert - jobs

Naive Pattern Searching using C++

This algorithm is used to search location of pattern in a string .
Example :
Input
string : My name is Bijendra kumar
pattern : Bijendra

Output
pattern is found at i=11;


Get Your Code


#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
cin.ignore();
while(t--)
{
string s,p;
getline(cin,s);
getline(cin,p);
int n,m;
n=s.length();
m=p.length();
int i,j;
for(i=0; i<=n-m; i++)
{
for(j=0; j<m; j++)
{
if(s[j+i]!=p[j])
{
break;
}




}
if(j==m)
{
cout<<"pattern is found at "<<i<<endl;

}
}
}
}

Follow us

Follow Bijendra Kumar on Facebook