字符替换:从键盘读入一个字符串,编程实现字符串中字符替换。要求编制函数 void replace(char *str, char a, char b); 将字符串str中的所有字符a全部替换为字符b。请编程实现对上述函数的调用。void main(){ char s[20], x = 'a', y='Z'; gets(s); //此处编写代码,调用上述函数replaceputs(s);
搜索字符串,找到字符a,换成字符b就行了
#include <stdio.h>
void replace(char *str, char a, char b)
{
int i=0;
while(str[i] != '\0')
{
if(str[i] == a)
str[i] = b;
i++;
}
}
void main()
{
char s[20], x = 'a', y='Z';
gets(s);
replace(s,x,y);
puts(s);
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!