|
#include<iostream.h>
#include<string.h>
//#define MAXSTRLEN 255
//typedef char string[MAXSTRLEN+1];
//using namespace std;
int find(char *str1,char *str2)
//S is the main string, T is the substring
{
int index=1;
char *p,*m,*q,*n;
char *Sstart,*Send,*Tstart,*Tend;
Sstart=str1;
Tstart=str2;
Send=Sstart+strlen(str2)-1;
Tend=Tstart+strlen(str2)-1;
p=Sstart;
m=Tstart;
q=p+strlen(str2)-1;
n=q+strlen(str2)-1;
if(strlen(str1)<strlen(str2))
{
cout<<"The substring is longer than the main string and cannot be matched!"<<endl;
return -1;
}//if
else
{
while(q<=str1+strlen(str1))
{
if(*p==*m&&*q==*n)
{
p++;q--;
m++;n--;
if(p=q)
{cout<<"matched successfully"<<endl<<"match from the first "<<index<<" characters of the main string"<<endl;break;}
The
}
else
{index++;
Sstart++;Send++;
The
The
if(Send>str1+strlen(str1)-1)
{cout<<"The match was not successful!";
break;
}
else
{
p=Sstart;q=Send;
m=Tstart;
n=Tend;
}
// cout<<"matched successfully!"<<endl<<"match from the "<<index<<" character of the main string!"<<endl;
}
The
}//while
}//else
return 1;
}
void main()
{
char str1[255],str2[255];
cout<<"Please enter the main string:";
cin>>str1;
cout<<endl;
cout<<"Please enter substring:";
cin>>str2;
cout<<endl;
find(str1,str2);
}
The
// cout<<find(str1,str2)<<endl;
I still have to rely on myself |
|