程序运行时出现“MotifSearch.exe遇到问题需要关闭”,不知怎样解决,请各位大侠帮帮忙!

nst1987 2009-04-18 10:51:04
下面是我编的一个程序,它的功能是在两个长字符串中找到内容相同的子串(也就是程序中所说的模体),比如在acctagtggaat和ggctctagaacg要找到ctag这个子串。程序每次运行的时候都出现“MotifSearch.exe遇到问题需要关闭,我们对此引起的不便感到抱歉”,我怎么检查都查不出错误,请各位大侠帮帮忙,看程序哪里有错误,感激不尽!
#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[]
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)
{
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);
}

...全文
91 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgjxwl 2009-04-18
  • 打赏
  • 举报
回复
#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);
}

zgjxwl 2009-04-18
  • 打赏
  • 举报
回复
strncpy(childstr,str2+k,N);  //将str2字符串中长度为N的子串复制到childstr[] 
childstr[4]='\0';//注意,字符数组要有'\0'标志的
zgjxwl 2009-04-18
  • 打赏
  • 举报
回复
while(j <N && i<Slen) //这里没给i上限制条件。。所导致的
liliangbao 2009-04-18
  • 打赏
  • 举报
回复
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);
}
「已注销」 2009-04-18
  • 打赏
  • 举报
回复
指针越界了。
下断点,逐步跟踪看看。
重点放在:MatchStr函数,感觉这里有问题
黄志义 2009-04-18
  • 打赏
  • 举报
回复
把代码整理一下再发 自己一步步跟踪调试 最好定位到出错的行
nst1987 2009-04-18
  • 打赏
  • 举报
回复
谢谢各位朋友帮忙,我的问题解决了!

65,189

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧