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; }