在这里给出函数被调用进行测试的例子。例如:
#include "stdio.h"
int replace(char *str,char ch1,char ch2) ;
main()
{
char str[81],ch1,ch2;
int count = 0;
gets(str) ;
scanf("%c %c",&ch1,&ch2);
coun = replace(str,ch1,ch2) ;
printf("replaced %d places\n",count);
printf("%s",str) ;
}
/* 请在这里填写答案 */
gfdsasdfgasdfaashgdk
a A
答案:
链表:
int replace(char *str, char ch1,char ch2 ) {
char *p=str;
int sum=0;
int i;
while(*p!='\0'){
if(*p==ch1){
*p=ch2;
sum++;}
p++;
}
return sum;
}
数组:
int replace(char *str, char ch1,char ch2 )
{
int i=0;
// char *p=str;
int count=0;
while(str[i]!='\0')
{
if(str[i]==ch1)
{
str[i]=ch2;
count++;
}
i++;
}
return count;
}