给定字符串,将其中的A字符串用B字符串代替

Kandada1985 2007-11-13 04:47:02
rt,最好能给出详细的代码,非常感谢!
...全文
104 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kandada1985 2007-11-14
  • 打赏
  • 举报
回复
to:C1053710211
能不能把你的方法描述一下呢?在此谢谢啦
Kandada1985 2007-11-14
  • 打赏
  • 举报
回复
非常感谢!
C1053710211 2007-11-14
  • 打赏
  • 举报
回复
用字符串函数strstr返回要被替换的字符串a在总字符串c中第一次出现的指针,然后构造一个新数组,
将这个指针之前的字符串拷过去,然后把要替换的字符串拷到新字符串后面,如此往复
CONGCONG1105 2007-11-14
  • 打赏
  • 举报
回复
Replace(str1,A,B)
C1053710211 2007-11-14
  • 打赏
  • 举报
回复
我们下午考试,现在恶补,等我晚上回来就给你解释。
C1053710211 2007-11-13
  • 打赏
  • 举报
回复
用最笨的方法写了一个,你看看能用不

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define len 1024
char *ans(char a[len],char b[len],char c[len])
{
char ans[len];
char *t = strstr(c,a);
char *p = c;
int i = 0,j;
int tlena = strlen(a);
int tlenb = strlen(b);
while(t!=NULL)
{
j=0;
while(p!=t)
{
ans[i++]=*p++;
j++;
}
ans[i]='\0';
strcat(ans,b);
j+=tlena;
i+=tlenb;
c+=j;
p=c;
t=strstr(c,a);
}
strcat(ans,c);
return ans;

}
int main()
{
char a[]="abc";
char b[]="de";
char c[]="aabcmmcabcnnabcd";
char *t;
t=strstr(c,a);
printf("a:%s\nb:%s\nc:%s\nanswer:%s\n",a,b,c,ans(a,b,c));
system("pause");
return 0;
}

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧