題目描述
輸入字符串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;
}