Title description
Suppose you are an officer in the legion, and you need to decipher the sent message and provide it to your general.
The method of message encryption is to replace each letter in the original message with the fifth letter after the letter (for example: each letter A in the original message is replaced with the letter F), and other characters remain unchanged. And all the letters of the original message are capitalized. The correspondence between the letters in the password and the letters in the original text is as follows.
Password letter: ABCDEFGHIJKLMNOPQRSTU VWXYZ
original letter: VWXYZABCDEFGHIJKLMNOP QRSTU
Input description: The
input includes multiple sets of data, one line for each set of data, which is the received ciphertext.
The ciphertext consists of only spaces and capital letters.
Output description:
corresponding to each group of data, output the decrypted plaintext.
Example
Input
HELLO WORLD
SNHJ
output
CZGGJ RJMGY
NICE
Problem solving ideas
Convert the original character into two parts, one part F-Z, only need to subtract 5 from the ASCII code; the other part A-E, add 21 to the ASCII code, and skip directly when encountering a space to complete the password interpretation.
Note: Enter a string with spaces to receive it with getline.
Complete code
#include <iostream>
#include <string>
using namespace std;
string Change(string s)
{
for (int i = 0; i < s.size(); i++)
{
//分别转化字母
if (s[i] >= 'F'&&s[i] <= 'Z')
s[i] -= 5;
else if (s[i] >= 'A'&&s[i] <= 'E')
s[i] += 21;
// 遇到空格直接跳过处理
else
continue;
}
return s;
}
int main()
{
string s;
//有空格的字符串要用getline 接收
while (getline(cin,s))
{
string s1 = Change(s);
cout << s1<<endl;
}
system("pause");
return 0;
}
Original title link
https://www.nowcoder.com/questionTerminal/9f6b8f6ec26d44cfb8fc8c664b0edb6b
Continue to follow the blogger~