题目描述
输入字符串s和字符c,要求去掉s中所有的c字符,并输出结果。
输入描述:
测试数据有多组,每组输入字符串s和字符c。
输出描述:
对于每组输入,输出去除c字符后的结果。
示例1
输入
复制
heallo
a
输出
复制
hello
题解
scanf在读取输入时会在缓冲区中留下一个字符'\n'(输入完s[i]的值后按回车键所致),所以如果不在此加一个getchar把这个回车符取走的话,gets就不会等待从键盘键入字符,而是会直接取走这个“无用的”回车符,从而导致读取有误。
#include <cstdio>
#include <string.h>
int main()
{
char s[100001];
char c;
while(gets(s)){
scanf("%c", &c);
int i;
for(i = 0; i < strlen(s); i++){
if(s[i] != c){
printf("%c", s[i]);
}
}
printf("\n");
getchar();
}
return 0;
}