PAT 1029 旧键盘
- 题目描述
- 思路讲解
- 代码展示
题目描述

思路讲解
分析:用string的find函数~遍历字符串s1,当当前字符s1[i]不在s2中,它的大写也不在ans中时,将当前字符的大写放入ans中,最后输出ans字符串即可~
代码展示
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
    string s1, s2, ans;
    cin >> s1 >> s2;
    for(int i = 0; i < s1.length(); i++)
    {
        if(s2.find(s1[i]) == string :: npos && ans.find(toupper(s1[i])) == string :: npos){
            ans += toupper(s1[i]);
        }
    }
    cout << ans;
}
s2.find(s1[i]) == string::npos:这部分判断是检查字符 s1[i] 是否不在字符串 s2 中。s2.find(s1[i]) 返回字符 s1[i] 在字符串 s2 中的位置索引,如果找不到(即不在 s2 中),则返回特殊值 string::npos(表示未找到)。因此,这个条件检查字符 s1[i] 是否不在 s2 中。


















