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;
}