提交记录 #397
提交时间:2024-12-03 16:25:25
语言:c
状态:Unaccepted
编译情况:编译成功
code.c: In function ‘replace’:
code.c:6:9: warning: unused variable ‘a’ [-Wunused-variable]
6 | int a=strlen(s);
| ^
固定测试点#1:
固定测试点#2:
固定测试点#3:
额外测试点#1:
52【字符】字符替换*——用指针更方便
#include <stdio.h>
#include <string.h>
void replace(char *s,char *t,char* str)
{
int a=strlen(s);
int b=strlen(t);
int c=strlen(str);
int j=0;
char *p=s;
char *q;
char k[100]={0};
while(*p!='\0'){
q=p;
int i=0;
while(*q!='\0'&&*t!='\0'&&*q==*t){
q++;
t++;
i++;
}
if(*t=='\0'&&i==b){
for(int m=0;m<c;m++){
k[j++]=str[m];
}
p+=i;
t-=i;
}else{
k[j++]=*p;
p++;
}t-=i;
}k[j]='\0';
strcpy(s,k);
}
int main()
{
char s[100];
char t[10];
char str[10];
fgets(s,sizeof(s),stdin);
int a=strlen(s);
if(a>0&&s[a-1]=='\n'){
s[a-1]='\0';
}
fgets(t,sizeof(t),stdin);
int b=strlen(t);
if(b>0&&t[b-1]=='\n'){
t[b-1]='\0';
}
fgets(str,sizeof(str),stdin);
int c=strlen(str);
if(c>0&&str[c-1]=='\n'){
str[c-1]='\0';
}
replace(s,t,str);
printf("%s\n",s);
return 0;
}