题解 | #字符串加解密#

字符串加解密

https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <algorithm>
#include <string>

using namespace std;


int main(){
	string str1, str2;
	cin >> str1 >> str2;

	// 加密
	for(int i =0;i<str1.size();++i){
		char c = str1[i];
		// 判断字母
		if(isalpha(c)){
			if (c >= 'a' && c <= 'z') {
				c = 'a' + (c - 'a'+1) % 26;
				c = toupper(c);
			}
			else {
				c = 'A' + (c - 'A' + 1) % 26;
				c = tolower(c);
			}
		}else if(isdigit(c)){
			c = '0' + (c - '0' + 1) % 10;
		}
		str1[i] = c;
	}
	
	// 解密
	for (int i = 0; i < str2.size(); ++i) {
		char c = str2[i];
		// 判断字母
		if (isalpha(c)) {
			if (c >= 'a' && c <= 'z') {
				c = 'z' - ('z' - c + 1) % 26;
				c = toupper(c);
			}
			else {
				c = 'Z' - ('Z' - c + 1) % 26;
				c = tolower(c);
			}
		}// 判断数字
		else if (isdigit(c)) {
			c = '9' - ('9' - c + 1) % 10;
		}
		str2[i] = c;
	}

	cout << str1 << endl;
	cout << str2 << endl;
}

全部评论

相关推荐

07-15 11:35
门头沟学院 Java
心里踏实多了,可以安心准备论文了
看不见我ffgh:牛哇佬,要不要来试一试pdd,部门氛围很好
京东开奖153人在聊
点赞 评论 收藏
分享
吴offer选手:下午mt一来就告警说项目来不及,估计明天拿了权限就要参与开发了 已老实
实习生的蛐蛐区
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务