Jimmy小站
小明也有大梦想 — 蒋明/铭8.4.3 编程题《自定义函数,删除字符串中的字符》
2015-08-30 / C语言基础题 / 5765 次围观 / 0 次吐槽【问题描述】
删除字符串中的字符。输入一个字符串s,再输入一个字符c,将字符串s中出现的所有字符c删除。要求定义并调用函数delchar(s,c),它的功能是将字符串s中出现的所有c字符删除。
【输入形式】
首先打印提示“Input a string:";然后直接在冒号后面输入字符串,字符串中可以包含空格;字符串以回车结束。
打印提示“Input a char:”;然后直接在冒号后面输入一个字符;回车。
【输出形式】
首先打印“After deleted,the string is:”;紧跟后面输出被删除后的字符串剩余内容;换行。
【运行时的输入输出样例】(下划线部分表示输入)
Input a string:happy new year
Input a char:a
After deleted,the string is:hppy new yer
#include <stdio.h> void delchar(char *str,char c) { char *p,*q; p=str; while(*p!='\0') { if(*p==c) { q=p; while(*q!=0) { *q=*(q+1); q++; } } if(*p!= c) p++; } } int main(void) { char s[100]; char c; printf("Input a string:"); gets(s); printf("Input a char:"); scanf("%c",&c); delchar(s,c); printf("After deleted,the string is:%s",s); return 0; }
推荐您阅读更多有关于“”的文章
本月热文
Copyright © Jimmy小站 Allrights Reserved.备案号:桂ICP备 15005996
额 本文暂时没人评论 来添加一个吧
发表评论