7、串替换:在主串S中查找子串T,若找到并把子串T替换为子串V */ int SStrReplace(SString *S,SString *T,SString *V) { int i,j; if(NULL==S||NULL==S->ch||NULL==T||NULL==T->ch||NULL==&V||NULL==V->ch) return FALSE; else{ for(i=0;i<S->length;i++) { _________________; //j用来存储,在串T中串S的位置 if(_________________) //判断是否具有替换的条件 { _________________; //SStrDelete(SString *S, int pos, int len),在串S中删除串T _________________; //SStrInsert(SString *S, int pos, const SString T),在串S中第j个位置开始插入串V } return TRUE; }
时间: 2024-03-02 14:51:23 浏览: 92
完整代码如下:
```c
int SStrReplace(SString *S, SString *T, SString *V) {
int i,j;
if(NULL==S||NULL==S->ch||NULL==T||NULL==T->ch||NULL==V||NULL==V->ch) {
return FALSE;
} else {
for(i=0;i<S->length;i++) {
j = 0; // j用来存储,在串T中串S的位置
while(j<T->length && i+j<S->length && S->ch[i+j]==T->ch[j]) {
j++;
}
if(j==T->length) { // 判断是否具有替换的条件
SStrDelete(S, i, T->length); // 在串S中删除串T
SStrInsert(S, i, V); // 在串S中第j个位置开始插入串V
i += V->length-1;
}
}
return TRUE;
}
}
```
解释如下:
1. 首先,判断传入的参数是否为空。
2. 然后,遍历主串S中的每一个字符。
3. 对于每个字符,从串T的开头开始匹配,直到匹配完所有的字符或者遇到不匹配的字符为止。
4. 如果匹配完所有的字符,说明找到了子串T,可以进行替换操作:先在主串S中删除子串T,再在删除子串T的位置插入子串V。插入完成后,将i移动到插入的子串V的末尾,以免重复匹配。
5. 遍历完成后,返回TRUE表示替换成功。
阅读全文
相关推荐















