题目:字符替换。要求用函数 replace 将用户输入的字符串中的字符 t(T)都替换为 e(E),并返回替换字符的个数。
代码实现:
/**< 字符替换。要求用函数 replace 将用户输入的字符串中的字符 t(T)都替换为 e(E),
并返回替换字符的个数 */
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
void replace(char *s);
int main()
{
char s[MAX]; //定义一个字符串数组,为字符串存储分配空间
printf("输入字符串:\n");
//scanf("%s", s);
gets(s); //字符串读取函数gets,在stdio.h中定义
replace(s);
puts(s);
return 0;
}
void replace(char *s)
{
while(*s != '\0')
{
if(*s=='t' || *s=='T')
{
*s = *s - 15;
}
s++;
}
}
运行结果:
本文详细介绍了如何使用C语言中的replace函数实现字符替换,并通过实例展示了其应用过程。
556

被折叠的 条评论
为什么被折叠?



