Codeforces:caps LOCK Uppercase and lowercase letter conversion problem

Source: Internet
Author: User
Tags lowercase uppercase letter

Convert the letters by following these rules:

Let's consider that's a word has been typed with the Caps lock key accidentally on, if:

Either it only contains uppercase letters;

or all letters except for the one are uppercase.

In this case we should automatically change the case to all letters. For example, the case of the letters that form words "HELLO", "HTTP", "Z" should to be changed.

Does a single uppercase letter require conversion? Need.

CBCD-> CBCD

Good-> good

H-> H

H-> H

It seems simple, but to solve this problem, to write the program gracefully, then it is not easy.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Look at my program, use automata knowledge, write a small automata system, gracefully solve this problem:

#include <iostream> #include <vector> #include <string> using namespace std;  
      
Enum Case {all_uppers, only_first_lower, correct, ilegal}; const static int Trans[6][2] = {{1, 2},//0 Init and Invalid {3, 5},//1 firs                 T Upper {4, 5},//2-lower {3, 5},//3 all upper {4, 5},              4 the other upper {5, 5}};  
    5 correct words case checkletters (string &s) {int state = 0;  
        for (int i = 0; i < s.size (); i++) {int input =-1;  
        if (' A ' <= s[i] && s[i] <= ' Z ') input = 0;  
        if (' A ' <= s[i] && s[i] <= ' z ') input = 1;  
        if ( -1 = = input) return ilegal;  
    state = Trans[state][input];  
    } if (1 = = state | | | 3 = state) return all_uppers; else if (2 = = State | | 4 = STATE) return only_first_lower;  
return correct; } void Transalllowers (String &s) {for (int i = 0; i < s.size (); i++) {S[i] = S[i  
    ]-' a ' + ' a ';  
    } void Transfirstupper (String &s) {S[0] = s[0]-' a ' + ' a ';  
    for (int i = 1; i < s.size (); i++) {s[i] = s[i]-' a ' + ' a ';  
    } void Capsloock () {string S;     
    cin>>s;  
    Case C = checkletters (s);  
    if (all_uppers = = C) transalllowers (s);  
    else if (Only_first_lower = = C) transfirstupper (s);  
    else if (Ilegal = = C) cout<< "Ilegal", exit (0);  
cout<<s;  
    int main () {capsloock ();  
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.