麦的答案

麦哥  •  1天前


#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string transform(int num, const string& s) {
    if (num == 1) {
        string res = s;
        for (char& c : res) c = toupper(c);
        return res;
    }
    else if (num == 2) {
        string res = s;
        for (char& c : res) c = tolower(c);
        return res;
    }
    else if (num == 3) {
        return string(s.rbegin(), s.rend());
    }
    else if (num == 4) {
        string res = s;
        for (char& c : res) {
            if (isupper(c)) c = tolower(c);
            else c = toupper(c);
        }
        return res;
    }
    else if (num == 5) {
        string str = s;
        for (char& c : str) c = tolower(c);

        string result = "";
        int n = str.size();
        int i = 0;
        while (i < n) {
            int start = i;

            while (i + 1 < n && str[i + 1] - str[i] == 1) {
                i++;
            }
            int length = i - start + 1;
            if (length >= 3) {
                result += str[start];
                result += '-';
                result += str[i];
            } else {
                result += str.substr(start, length);
            }
            i++;
        }
        return result;
    }
    return s;
}

int main() {
    int num;
    string s;
    cin >> num >> s;
    cout << transform(num, s) << endl;
    return 0;
}

include

include

include

using namespace std;

string transform(int num, const string& s) {

if (num == 1) {
    string res = s;
    for (char& c : res) c = toupper(c);
    return res;
}
else if (num == 2) {
    string res = s;
    for (char& c : res) c = tolower(c);
    return res;
}
else if (num == 3) {
    return string(s.rbegin(), s.rend());
}
else if (num == 4) {
    string res = s;
    for (char& c : res) {
        if (isupper(c)) c = tolower(c);
        else c = toupper(c);
    }
    return res;
}
else if (num == 5) {
    string str = s;
    for (char& c : str) c = tolower(c);

    string result = "";
    int n = str.size();
    int i = 0;
    while (i < n) {
        int start = i;

        while (i + 1 < n && str[i + 1] - str[i] == 1) {
            i++;
        }
        int length = i - start + 1;
        if (length >= 3) {
            result += str[start];
            result += '-';
            result += str[i];
        } else {
            result += str.substr(start, length);
        }
        i++;
    }
    return result;
}
return s;

}

int main() {

int num;
string s;
cin >> num >> s;
cout << transform(num, s) << endl;
return 0;

}



评论:

请先登录,才能进行评论