题解 | #字符串加解密#

字符串加解密

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;
}

全部评论

相关推荐

09-12 11:00
门头沟学院 Java
b溃了,早知道不拉扯了
在迎接offer的废...:手中握着有一两个offer才敢拉扯,没保底就别了吧。
我的秋招日记
点赞 评论 收藏
分享
09-13 17:43
已编辑
北京化工大学 硬件开发
易才一飞:感觉项目写细节一些吧,掌握技能和校内经历感觉占比太大,而且这是找嵌软还是硬件呢,似乎大家都说要有针对的写相关技术才好吧
点赞 评论 收藏
分享
09-12 18:28
门头沟学院 Java
网友描述的太精准了👍
迷茫的大四🐶:不管活脏还是累,钱到位就行,钱到位啥都不用抱怨
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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