跳到正文

题解

题解:字符串 / String

xinyu12345约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年9月26日
收录
2026年9月26日
更新
2026年11月10日
篇幅
约 2 分钟

版本

  1. v1当前

题目传送门

思路

直接模拟即可,根据题目,需要做如下操作:

  1. 遍历字符串,对于 si (0≤i<n)s_i\ (0 \le i < n),如果其为小写字母(islower(s[i])),则转大写,反之转小写字母。
  2. 删去元音字母(a,e,i,o,u 和 A,E,I,O,U)。
  3. 反转字符串。

代码

C++
#include <bits/stdc++.h>
using namespace std;
bool chk(char c) {
    return c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U';
}
int main() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    string ans;
    for (char c : s) {
        if (chk(c)) continue;
        if (islower(c)) {
            ans += toupper(c);
        } else if (isupper(c)) {
            ans += tolower(c);
        }
    }
    reverse(ans.begin(), ans.end());
    cout << ans;
    return 0;
}
原文