#include<iostream>
using namespace std;
char c[300];
int main(){
string a,b;
getline(cin,a);
getline(cin,b);
for(int i=0;i<a.length();i++){
c[a[i]]=1; //如果有这个错误字符,就把这个字符标记为1
if(a[i] >= 'A' && a[i] <= 'Z')
c[a[i]-'A'+'a']=1; //如果这个字符是大写字母,把小写字母也标记为1
if(c['+'] == 1){ //如果读入'+',把所有大写字母都标记为1
for(int l='A';l<='Z';l++) c[l]=1;
}
}
for(int i = 0; i < b.length(); i++){
if(c[b[i]] != 1) cout<<b[i];
}
cout<<endl;
return 0;
}
大佬的代码
#include <iostream>
#include <cctype>
using namespace std;
int main() {
string bad, should;
getline(cin, bad);
getline(cin, should);
for (int i = 0, len = should.length(); i < len; i++) {
if (bad.find(toupper(should[i])) != string::npos) continue;
if (isupper(should[i]) && bad.find('+') != string::npos) continue;
cout << should[i];
}
return 0;
}
分析:坏掉的键保存在字符串bad中,应该输入的文字保存在should中,遍历整个应该输入的字符串。
因为坏键以大写给出,所以如果在bad里面找到了should[i]的大写,表示这个字符对应的键坏了,则跳过这个字符不输出,continue跳过~
如果should[i]是大写并且在bad中找到了’+’,表示上档键坏了,大写无法输出,所以这个字符也不能输出,continue跳过~
如果都没跳过,则要输出should[i]~
本文介绍了一种通过算法过滤键盘故障字符的方法,使用C++实现,包括两种代码示例。一种使用字符数组标记法,另一种利用string的find方法检查坏掉的键。文章详细解析了代码逻辑,适用于处理文本输入中特定字符的排除问题。
1267

被折叠的 条评论
为什么被折叠?



