代码如下:
[cpp]
#define MAX 200
int main(void)
{
int i = 0;
char s[MAX] ={0}; //存放源字串
char s1[MAX]={0}; //存放子字串
char s2[MAX]={0}; //存放替换字串
char result_a[2000] = {0};//存放替换结果;
char *p,*ptm,*pr;
puts("Please input the string for s:");
scanf("%s",s);
puts("Please input the string for s1:");
scanf("%s",s1);
puts("Please input the string for s2:");
scanf("%s",s2);
ptm = s;
pr = result_a;
i = str_replace(pr,ptm,s1,s2);
printf("替换%d个子字符串;\r\n",i);
printf("替换后结果:%s\r\n",result_a);
system("pause");
}
运行结果如果:
[cpp]
Please input the string for s:
123123123123
Please input the string for s1:
23
Please input the string for s2:
abcdefg
结果: www.2cto.com
源字符:123123123123
结果:1abcdefg
源字符:123123123123
结果:1abcdefg1abcdefg
源字符:123123123123
结果:1abcdefg1abcdefg1abcdefg
源字符:123123123123
替换4个子字符串;
替换后结果:1abcdefg1abcdefg1abcdefg1abcdefg
请按任意键继续. . .