Getting exercises 8 string case conversion lock solution to a problem

Written for: http://codeforces.com/problemset/problem/131/A

Title Description

For a string s, its case conversion lock is the goal for every character string s, case conversion get.
(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

Contains the string input s (length between 1 and 100 s) is composed of only a case letters.

Output Format

S output in accordance with the subject described in the original string.

Sample input

cAPS

Sample Output

Caps

Sample input

Lock

Sample Output

Lock

Topic analysis

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/zifeiynoip/p/11450594.html