Question: Caesar caesar password (c ++ implementation) and Caesar caesar
Description: Julius Caesar lived in an age full of danger and conspiracy. To survive, he invented the password for the first time for military message transmission. Assume that you are an officer in the Caesar army. You need to decrypt the message sent by Caesar and provide it to your general. The message encryption method is to replace each letter in the original message with the first letter after the letter (for example, each letter A in the original message is replaced with the letter F ), other characters remain unchanged, and all letters in the original message are in uppercase. Password letter: a B c d e f g h I J K L M N O P Q R S T U V W X Y Z original letter: V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
Input: consists of up to 100 datasets. Each data set consists of three parts: START line: START password message: a line consisting of 1 to 200 characters, indicating the END line of a message sent by Caesar: END after the last data set, is another row: ENDOFINPUT
Output: each dataset corresponds to a row, which is the original message of Caesar.
Input:
STARTNS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJXENDSTARTN BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJENDSTARTIFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJENDENDOFINPUT
ouput:
In war, events of importance are the result of trivial causesi wocould RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROMEDANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE
Analysis: The question is relatively simple. It should be noted that the input function is used. The password message contains spaces, so cin cannot be used for input. Therefore, getline (cin. str) Input. In particular, getline () must be used for the first start string. Otherwise, the password is blank. I think the carriage return character is still in the buffer zone after the start is input and the line feed is used, when getline () is used, the input is read by the carriage return, so that the password message is blank.
Getline (), with only the carriage return as the Terminator
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 void change(string& a) 6 { 7 for (int i = 0; i < a.size(); i++) 8 { 9 if (a[i] >= 'A'&&a[i] <= 'U')10 a[i] += 5;11 else if (a[i] >= 'V'&&a[i] <= 'Z')12 a[i] = 'A' + a[i] - 'V';13 }14 }15 16 int main()17 {18 string start, message, end;19 while ((getline(cin, start)) && (start.compare("ENDOFINPUT")))20 {21 getline(cin,message);22 getline(cin, end);23 change(message);24 cout << message << endl;25 }26 system("pause");27 return 0;28 }