OCAC summer tournament first question I lock case conversion solution to a problem

Case Conversion locks
original title link: http: //codeforces.com/problemset/problem/131/A
[Description] title
for a string s, its case conversion lock every one pair of s after the character case conversion to get the destination string.
(That is, for every s top character c, if c is an uppercase letter, it is converted into the corresponding lowercase letter; if c is a lowercase letter, it is converted to uppercase)
but not all strings are encrypted, we define a string case conversion lock, if and only if:
1, this string are all uppercase letters;
2, or in addition to the first letter of the string is not a capital letter, other letters are capital letters.
Give you a string, you need to determine what its original string Yes.
This means that if you convert the string is not case-sensitive password lock, you just need the output as it can;
and if you convert strings are case lock, you need to output its original string.
[Input format
input string contains s (length between 1 and 100 s) is composed of only a case letters.
[] Output format
of the original string s output in accordance with the subject described.
Sample input [1]
cAPS
[1] sample output
Caps
[2] sample input
Lock
[2] sample output
Lock
[analysis] title
This is a very simple problem, we only need to determine other letters except the first letter is not all uppercase, we can determine that it is not the case conversion lock up.
Then, if it is the case conversion lock, we look at the case against him transformed it.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;

string s;

bool check() {
    int n = s.length();
    for (int i = 1; i < n; i ++) {
        if (s[i] >= 'a' && s[i] <= 'z') return false;
    }
    return true;
}

void transfer() {
    int n = s.length();
    for (int i = 0; i < n; i ++) {
        if (s[i] >= 'a' && s[i] <= 'z') s[i] = toupper(s[i]);
        else s[i] = tolower(s[i]);
    }
}

int main() {
    cin >> s;
    if (check()) transfer();
    cout << s << endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/ocac/p/11113443.html