baidu_24528529 2014-12-21 06:46 采纳率: 0%
浏览 1596

求大神帮忙,把两个程序合成一个,既求出位置,又替换。

1:#include
#include
#include
void main()
{
char strSource[6000],strFind[100];
puts("母串为:");
gets(strSource);
puts("要查找的子字符串为:");
gets(strFind);
cout<<"母串为:"< cout int i,t=0,l1,l2,k,f;
l1=strlen(strSource);
l2=strlen(strFind);
for(i=0;i {
k=0;
f=1;
while(f)
{
if(*(strSource+i+k)!=*(strFind+k))
f=0;
else
f=f+1;
k++;
if(f==l2+1)
{
t++;
f=0;
cout }
}
i+=k;
}
if(t==0)
cout }
2:#include
#include
#include
void exchange(char*strSource,char*strFind,char*strReplace)
{
int i,r,d,l1,l2,l3,k,f;
l1=strlen(strSource);
l2=strlen(strFind);
l3=strlen(strReplace);
d=l3-l2;
for(i=0;i {
k=0;
f=1;
while(f)
{
if(*(strSource+i+k)!=*(strFind+k))
f=0;
else
f=f+1;
k++;
if(f==l2+1)
{
if(d>0)
for(r=0;r<=l1-i-l2;r++)
(strSource-r+d+l1)=(strSource+l1-r);

if(d<0)
for(r=0;r<l1-i-l2+1;r++)
(strSource+i+l2+d+r)=(strSource+i+l2+r);

for(r=0;r<l3;r++)
(strSource+i+r)=(strReplace+r);
l1=l1+d;
f=0;
k=l3;

}
}

i+=k;

}
}
void main()
{

char strSource[6000],strFind[100],strReplace[100];
puts("母串为:");
gets(strSource);
puts("被替换的子字符串为:");
gets(strFind);
puts("子字符串被替换为:");
gets(strReplace);
exchange(strSource,strFind,strReplace);
cout<<"母串为:"<<strSource<<endl;
cout<<"被替换的子字符串为:"<<strFind<<endl;
cout<<"子字符串被替换为:"<<strReplace<<endl;
cout<<"替换后母串为:"<<strSource<<endl;
}

  • 写回答

2条回答

  • 拥抱开源 2014-12-21 09:54
    关注

    你写的太复杂了啊 可以直接调用函数的

    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示