您的位置:首页 > 其它

删除字符串中的子串

2012-06-14 15:28 232 查看
利用串的基本运算,编写一个算法删除串s1中所有的s2子串

 

void index(strtype *s1,strtype *s2);

void search(strtype *s1,strtype *s2,int i)

{

 if(i>=len(s1))

  return ;

 else

 {

  int k=i,j=0;

  int m=k;

  while(s1->ch[k]==s2->ch[j] && j<=len(s2))

  {

   k++;

   j++;

  }

  if(j>=len(s2))

  {

  index(del(s1,m+1,len(s2)),s2);

  }

  else

   search(s1,s2,i+1);

 }

}

void index(strtype *s1,strtype *s2)

{

 int t=0;

 if(len(s1)<len(s2))

  return ;

 int i=0;

 while(i<len(s1) && s1->ch[i]!=s2->ch[0])

 {

  i++;

 }

 if(i==len(s1))

  return ;

 else if(i<len(s1))

 {

  search(s1,s2,i);

  return ;

 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  search 算法