65,189
社区成员




#include <iostream>
#include <string>
using namespace std;
int MatchStr(char S[],char T[],int Slen,int N);
int main()
{
int k=0,strlen1,strlen2,position1,position2;
char str1[]="ACCTAGTGGAAT",str2[]="GCTAGCCTAACG",childstr[20];
int N=4; //N为要寻找的模体的长度
strlen1=strlen(str1);
strlen2=strlen(str2);
while(k <strlen2-N+1)
{
strncpy(childstr,str2+k,N); //将str2字符串中长度为N的子串复制到childstr[]
childstr[4]='\0';//注意,字符数组要有'\0'标志的
position1=MatchStr(str1,childstr,strlen1,N); //position1为模体在第一个字符串中的起始位置
if(position1!=-1)
break;
k++;
}
if(k>=strlen2-N+1)
cout <<"没有找到长度为" <<N <<"的模体" <<endl;
else
{
position2=k+1;
cout <<"找到的模体为:" <<childstr <<endl;
cout <<"其在第一个序列中的起始位置为:" <<position1 <<endl;
cout <<"其在第二个序列中的起始位置为:" <<position2 <<endl;
}
return 0;
}
//查找字符串T在字符串S中的位置
int MatchStr(char S[],char T[],int Slen,int N)
{
int i=0,j=0;
while(j <N && i<Slen) //这里没给i上限制条件。。所导致的
{
if(S[i]==T[j])
{
i++;
j++;
}
else
{
i=i-j+1;
j=0;
}
}
if(i>=Slen&&j <N)
return -1;
else
return (i-j+1);
}
strncpy(childstr,str2+k,N); //将str2字符串中长度为N的子串复制到childstr[]
childstr[4]='\0';//注意,字符数组要有'\0'标志的
while(j <N && i<Slen) //这里没给i上限制条件。。所导致的
int MatchStr(char S[],char T[],int Slen,int N)
{
int i=0,j=0;
while(j <N)
{
if(S[i]==T[j])
{
i++;
j++;
}
else
{
i=i-j+1; //修改为i++
j=0;
}
}
if(i>=Slen&&j <N)
return -1;
else
return (i-j+1);
}