BUPT-homework/pset13/1-6-DelChar.c

34 lines
726 B
C
Raw Normal View History

2021-12-09 17:26:31 +08:00
#include <stdio.h>
void delcharfun(char *str, char ch);
int main() {
char ch, str[110];
scanf("%s", str); //读入字符串
getchar(); //读取回车符号
scanf("%c", &ch); //读入字符
delcharfun(str, ch); //删除
printf("%s\n", str); //输出删除后结果
return 0;
}
/* 请在这里填写答案 */
void delcharfun(char *str, char ch) {
int i;
while (*str != '\0' && *str != '\n') {
if (*str == ch) {
// Shift this array
i = 0;
while (*(str + i + 1) != '\0') {
*(str + i) = *(str + i + 1);
i++;
}
// End this string to avoid duplicates
*(str + i) = '\0';
str--; // Because the string is shifted
}
str++;
}
}