C语言编程(练习7:数组与指针)

来源:互联网 发布:ins是什么社交软件 编辑:程序博客网 时间:2024/04/30 12:55

题目:字符替换。要求用函数 replace 将用户输入的字符串中的字符 t(T)都替换为 e(E),并返回替换字符的个数。

代码实现:

/**< 字符替换。要求用函数 replace 将用户输入的字符串中的字符 t(T)都替换为 e(E),并返回替换字符的个数 */#include <stdio.h>#include <stdlib.h>#define MAX 100void 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++;    }}

运行结果:


0 0
原创粉丝点击