3445. 字符串替换

一个句号

void replace(char s[],char x[],char y[]){
int i,j,k;
i=j=k=0;int ai,bi;
while(s[i]!=x[j])i++;
ai=bi=i;
while(s[i]=x[j]){
i++;j++;
}
if(j==strlen(x)){
for(;ai<bi+strlen(x);ai++){
s[ai]=y[k++];
}
}

}
为什么这样写,替换字符串后面的字符输出不了了?

Kevin_K

可以,这道题刷新了我对字符串替换的认知,字符串原来也是可以长得“奇形怪状”(比如说将x替换为y,x可以等于y,当然这题数据里还有更加“奇形怪状”的东西啦)的啊。233~

1574080260

include

include

//* Specification of replace *
void replace(char s[], char x[], char y[]);
/ Precondition: s, x and y are three strings,
and s has enough memory to store modified string
Postcondition: replace all substring x with y in string s
/

void replace(char s[], char x[], char y[]) { //TODO: your function definition
int chang_x = strlen(x);
int chang_s = strlen(s);
char z[500]= {‘\0’,’\0’};
int i,k=0;
for(i=0; i<chang_s; i++){
if(s[i] == x[0]){
goto copy;
}
rrestar: z[k] = s[i];
k++;
restar: ;
}
strcpy(s,z);
return;
int j, place, right;
copy: place = i, right = 1;

    for(j=0; j<chang_x; j++){
        if(i == chang_s -1 && i != 0 && chang_s % chang_x != 0){
            i = place;
            goto rrestar;
        }
        if(s[i] != x[j]){
            right = 0;
        }
        i++;
    }
    if(right == 0){
            i = place;
            goto rrestar;
        }
    if(right == 1){
        int j=0, k_ = k;
        while(y[j] != '\0'){
                z[k] = y[j];
                j++;
                k++;
        }
        i = place + chang_x - 1;
        goto restar;
}

}

define N 80

int main() {
char s[3 * N + 1], x[N + 1], y[N + 1];
scanf(“%s%s%s”, s, x, y);
replace(s, x, y);
printf(“%s\n”, s);
return 0;
}

Stevenaa

import re
obj=input().split()
repl=re.sub(r’%s’%obj[1],obj[2],obj[0])
print(repl)
‘’‘/doge’‘’

LzQuarter

aaa a b

童雯雯

include

using namespace std;
string s,t,r;
int lens,lent,tmp,cnt,flag;
int i,j;
int main()
{
cin>>s>>t>>r;
lens=s.length(),lent=t.length();
if(lent>lens)
{
cout<<s<<endl;
return 0;

}
tmp=lens-lent+1;
for(i=0;i<tmp;++i)
{
    if(s[i]==t[0])
    {
        for(cnt=j=1;j<lent;++j)
        {
            if(s[i+j]!=t[j])
            {
                cnt=0;
                break;

            }
        }
        if(cnt)
        {
            cout<<r;
            i+=lent-1;
            flag=1;
        }
        else cout<<s[i],flag=0;
    }
    else cout<<s[i],flag=0;
}
if(flag)
    for(j=i;j<lens;++j)
        cout<<s[j];
else 
    for(j=tmp;j<lens;++j)
        cout<<s[j];
cout<<endl;
return 0;

}

1574080260

include

include

//* Specification of replace *
void replace(char s[], char x[], char y[]);
/ Precondition: s, x and y are three strings,
and s has enough memory to store modified string
Postcondition: replace all substring x with y in string s
/

void replace(char s[], char x[], char y[]) { //TODO: your function definition
int chang_x = strlen(x);
int chang_s = strlen(s);
char z[500]= {‘\0’,’\0’};
int i,k=0;
for(i=0; i<chang_s; i++){
if(s[i] == x[0]){
goto copy;
}
rrestar: z[k] = s[i];
k++;
restar: ;
}
strcpy(s,z);
return;
int j, place, right;
copy: place = i, right = 1;

    for(j=0; j<chang_x; j++){
        if(i == chang_s -1 && i != 0 && chang_s % chang_x != 0){
            i = place;
            goto rrestar;
        }
        if(s[i] != x[j]){
            right = 0;
        }
        i++;
    }
    if(right == 0){
            i = place;
            goto rrestar;
        }
    if(right == 1){
        int j=0, k_ = k;
        while(y[j] != '\0'){
                z[k] = y[j];
                j++;
                k++;
        }
        i = place + chang_x - 1;
        goto restar;
}

}

define N 80

int main() {
char s[3 * N + 1], x[N + 1], y[N + 1];
scanf(“%s%s%s”, s, x, y);
replace(s, x, y);
printf(“%s\n”, s);
return 0;
}

太菜了怎么办

“s has enough memory to store modified string”
假的吧= = 还是我理解错了TuT

Luckywei233

include

define N 80

int slen(char p){
int i=0;
while(p[i]!=’\0’){
i++;
}
return i;
}
void strcopy(char
a,char b){
for(int i=0;i<=slen(a);i++){
b[i]=a[i];
}
}
char temp[3
N+1];
char substr(char p,int a,int b){
int i;
for(i=a;i<=b;i++){
temp[i-a]=p[i];
}
temp[i-a]=’\0’;
return temp;
}
int equals(char a,char b){
if(slen(a)==slen(b)){
for(int i=0;i<=slen(a)-1;i++){
if(a[i]!=b[i])return 0;
}
return 1;
}else{
return 0;
}
}
void replace(char s[], char x[], char y[]) {
char result[3*N+1];
int index=0;//TODO: your function definition
for(int i=0;i<=slen(s)-1;i++){
if(s[i]==x[0]&&equals(substr(s,i,i+slen(x)-1),x)){
for(int j=0;j<=slen(y)-1;j++){
result[index]=y[j];
index++;
}
i+=slen(x)-1;
}
else{
result[index]=s[i];
index++;
}
}
result[index]=’\0’;
strcopy(result,s);
}
int main() {
char s[3 * N + 1], x[N + 1], y[N + 1];
scanf(“%s%s%s”, s, x, y);
replace(s, x, y);
printf(“%s”,s);
return 0;
}

你当前正在回复 博客/题目
存在问题!