你的代码贴出来看看
我是用的C标准库中的函数来完成的寻找替换操作,不知道满足你的要求不,代码如下:
#include <stdio.h>
#include <string.h>
int main(void){
char s[100],f[100],r[100],temp[100];
char * position,*ts;
scanf("%s",s);
ts=s;
// printf("s=%s\n",s);
scanf("%s",f);
//printf("f=%s\n",f);
scanf("%s",r);
// printf("r=%s\n",r);
//如果在字符串中找到替换字符串f,则进行替换操作
while((position=strstr(ts,f))!=NULL){
strcpy(temp,position+strlen(f)); //将字符串s中替换当前字符串后面的字符串备份到临时字符数组tmep中
strcpy(position,r); //把r字符串复制到字符串s中寻找到的这个位置
strcpy(position+strlen(r),temp); //在字符串s的刚复制的r字符串后面追加原来尾部的字符串
ts=position+strlen(r); //从字符串s刚复制的r字符串后面开始下一次寻找
}
printf("%s",s); //打印结果
return 0;
}