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 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题