要求:编写void fun(char s[], char c)函数,其功能是删除一个字符串s中指定的字符c。在main函数中输入字符串和要删除的字符,调用函数fun后,最后输出结果。如果输入的字符在字符串中不存在,输出格式:("%c not exist in\n",c),并输出原字符串。
【样例输入】
Hello world!
o
【样例输出】
Hell wrld!
主函数:
#include<stdio.h>
void fun(char s[ ], char c);
int main() {
char str[50], c;
gets(str);
scanf("%c", &c);
fun(str, c);
return 0;
}
fun(char s[ ], char c):
void fun(char s[], char c) {
int i, k = 0, del = 0;
for (i = 0; s[i] != '\0'; i++) {
if (s[i] != c) {
s[del] = s[i];
del++;
}
}
k = i;
s[del] = '\0';
if (i != del) { //判断前后有效字符串长度是否相等
puts(s);
}
else {
printf("%c not exist in\n",c);
puts(s);
}
}