编辑代码

#include<stdio.h>
/*replace(char *s,char c1,char c2){
while(*s!='\0'){
if (*s==c1)
*s=c2;
s++;
}
}*/

replace(char s[],char c1,char c2){
int len=sizeof(s);
int i;
for(i=0;i<len;i++){
if (s[i]==c1)
s[i]=c2;
}
}

main(){
FILE *fp;
char str[100],a,b;
if((fp=fopen("p10_2.out","w"))==NULL){
printf("cannot open the file\n");
exit(0); 
}
printf("Enter a string:\n");
gets(str);
printf("Enter a&&b:\n");
a=getchar();
b=getchar();
printf("%s\n",str);
fprintf(fp,"%s\n",str);
replace(str,a,b);
printf("The new string is----%s\n",str);
fprintf(fp,"The new string is----%s\n",str);
fclose(fp);
}