void Replace(StringType &S, StringType T, StringType V)
{
int n,m,k,i;
StringType sub;
InitStr(sub);
n = StrLength(S);
m = StrLength(T);
k = StrLength(V);
i = 1;
while(i <= n-m+1){
StrAssign(sub,SubString(S,i,m));//将串C中的子串逐个提取出来与串T进行匹配
if(StrCompare(sub,T) == 0){//匹配相等时
InitStr(sub);
Concat(sub,SubString(S,1,i-1));//将匹配相等的子串的前面的子串连接到串sub后
Concat(sub,V);//将替换串连接到串sub后
Concat(sub,SubString(S,m+i,n-(m+i)+1));//将匹配相等的子串的后面的子串连接到串sub后
StrAssign(S,sub);
i += k;
n = StrLength(S);//操作完一次之后,串S已经改变,需要注意
}
else{
++i;
}
}
}
温馨提示:答案为网友推荐,仅供参考