|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1. 以下程序中函数replace的功能是:将字符串s中所有属于字符串s1中的字符都用s2中的对应位置的字符替换。假如s为“ABCBA”,s1为“AC”,s2为“ac”,则调用replace函数后,字符串s的内容将变换为“aBcBa”。试完善程序以达到要求的功能。
#include <stdio.h>
#define MAX 20
void replace(char *s, char *s1, char *s2)
{ char *p;
for(; *s; s++)
{ p=s1;
while(*p&& *p!=*s ) p++;
if(*p) *s= *s=*(s2+(p-s1)) ; // 就是这个地方,为啥写成*(s2+p-s1)就不能行了呢?
}
}
void main( )
{ char s[MAX]="ABCBA",s1[MAX]="AC", s2[MAX]= "ac";
replace(s, s1, s2);
printf("The string of s is:");
printf("%s\n", s);
}
本帖最后由 WindyJane 于 2021-1-7 21:24 编辑
(p-s1+s2) 偏移+地址= (s2+(p-s1))地址+偏移 √
地址+地址 -地址 ×
|
|