1298: The Hardest Problem Ever: string class fast processing character conversion

Title Translation

Julius Caesar live in a time full of danger and intrigue. The most difficult situation faced by Caesar is to save their lives. To survive, he decided to create a password first. This password is so incredible sound, people do not know how it works.

You are vice-captain of Caesar's army. Your job is to decipher the information sent Caesar and provide it to you, General. The code is simple. For each letter in the plaintext message, move it to the right position to create 5 security message (i.e., if the letter is "A", then the ciphertext is "F"). Because you are creating a plain text message from Caesar, you will perform the reverse operation

Cipher text 
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 

Plain text 
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 

This password only letters shift. Any non-alphabetic character should remain unchanged, all alphabetic characters should be capitalized.

Entry

Input to this problem will contain a (non-empty) series, up to 100 data sets. Each set of data will be formatted as described below, the blank line does not exist between the data sets. All characters are capitalized.

A single data set contains three components:

  • Start line - a line, "START"
  • Password message - row comprises from one to two hundred characters (inclusive), including a message from Caesar
  • The end of the line - a line, "END"

After the last data set is a line of "ENDOFINPUT".

Export

For each data set, there will be only one line of output. This is the first information Caesar.

Ideas analysis

string class very good operation, getline(cin,str)reads a line of characters (remember omitting leading space), then for each character in turn can be processed.

#include<iostream>
#include<map>
#include<string>
#include<string.h>
using namespace std;

int main() {
	string s1, s2, s3;
	while (cin >> s1 && s1 != "ENDOFINPUT") {
		if (s1 == "END")continue;
		cin.get();//略去前导回车
		getline(cin, s2);
		s3.assign(s2);
		for (unsigned i = 0; i < s2.size(); i++) {
			if (s2[i] >= 'A'&&s2[i] <= 'Z') {
				if (s2[i] >= 'A' + 5) s3[i] = s2[i] - 5;
				else s3[i] = s2[i] - 5 + 26;
			}
			else s3[i] = s2[i];
		}
		cout << s3 << endl;
	}
}
Published 186 original articles · won praise 13 · views 9306

Guess you like

Origin blog.csdn.net/csyifanZhang/article/details/105214974