题目:从控制台输入一串字符串,要求删除指定的字符
#include<stdio.h>
#include<string.h>
void removeChar(char *str, char toRemove);
int main(){
char str[100];
char toRemove;
printf("请输入一个字符串(不超过99个字符): ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0;
printf("请输入要删除的字符: ");
scanf(" %c", &toRemove);
removeChar(str, toRemove);
printf("删除'%c'后的字符串: %s\n", toRemove, str);
return 0;
}
void removeChar(char *str, char toRemove){
char *src = str, *dst = str;
while(*src){
if(*src != toRemove){
*dst = *src;
dst++;
}
src++;
}
*dst = '\0';
}