POJ-2141-Message Decowding-解题报告

注意运用getline函数来获得一行字符串。

 

#include <iostream> #include <string> #include <ctype.h> using namespace std; int main() { string keyStr, msgDecStr, aLine; getline(cin, aLine); keyStr = aLine; const char *key = keyStr.c_str(); getline(cin, aLine); msgDecStr = aLine; const char *msgDec = msgDecStr.c_str(); char msg[100]; char c, c2, c3; unsigned int i; for(i = 0; i < msgDecStr.length(); ++i) { c = msgDec[i]; if(isupper(c)) { c2 = key[c - 'A']; c3 = toupper(c2); msg[i] = c3; } else if(c == ' ') { msg[i] = c; } else { c2 = key[c - 'a']; msg[i] = c2; } } msg[i] = '/0'; cout << msg << endl; return 0; }

你可能感兴趣的:(c,String)