题解 | #字符串加解密#

字符串加解密

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

全部评论

相关推荐

程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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