一个字符串经过多次译码替换后得到一个新目的串,目的串的长度可能长于,也可能短于源串
运行时间:无限制 内存限制:无限制
输入:源字符串:abcdefgh
替换字符串数组:2
替换字符串:ab->m
ef->xyzq
替换后输出结果:mcdxyzqgh
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
void trans(char *str1,char*arry1,char *arm2,char *output )
{
int number=0,num=0;
size_t j=0;
char *begin=str1,*outbegin=output;
char *Inout=(char *)malloc(sizeof(char)*100);
char *sqw=Inout;
char temp1[20];//存储比较量arry1 ->前面
char temp2[20];//存储源字符串比较量
char temp3[20];//存储arm2的字符串
memset(temp1,'\0',20);
memset(temp2,'\0',20);
memset(temp3,'\0',20);
memset(Inout,'\0',100);
number=word(arry1);
num=word(arm2);
memcpy(temp1,arry1,number);
memcpy(temp3,arm2,num);
while(*str1)
{
memcpy(temp2,str1,number);
if(strcmp(temp1,temp2)==0)
{
for(j=number+2;j<strlen(arry1);j++)
*output++=arry1[j];
str1=str1+number;
}
else
{
*output++=*str1++;
memset(temp2,'\0',20);
}
}
str1=outbegin;
while(*str1)
{
memcpy(temp2,str1,num);
if(strcmp(temp2,temp3)==0)
{
for(j=num+2;j<strlen(arm2);j++)
*Inout++=arm2[j];
str1=str1+num;
}
else
{
*Inout++=*str1++;
memset(temp2,'\0',20);
}
}
printf("%s",sqw);
// free(Inout);
}
int word(char *arry2)
{
int i=0,sa=0;
for(i=0;i<strlen(arry2);i++)
if(arry2[i]=='-'&&arry2[i+1]=='>')
sa=i ;
return sa;
}
void main()
{
char str[30];//原来的串
char arry[20];//替换1
char arm[20];//替换2
int num=0;
char *out=(char *)malloc(sizeof(char)*100);
memset(out,'\0',100);
scanf("%s",&str);
scanf("%d",&num);
scanf("%s\n%s",&arry,&arm);
trans(str,arry,arm,out);
//while(1);
}
自己用C写的 ,只考虑了测试样例 ,代码中间可优化,完了有时间再改