首页 > 试题广场 >

字符串加解密

[编程题]字符串加解密
  • 热度指数:252134 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

对输入的字符串进行加解密,并输出。

加密方法为:

当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;

当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;

其他字符不做变化。

解密方法为加密的逆过程。
数据范围:输入的两个字符串长度满足 ,保证输入的字符串都是只由大小写字母或者数字组成

输入描述:

第一行输入一串要加密的密码
第二行输入一串加过密的密码



输出描述:

第一行输出加密后的字符
第二行输出解密后的字符

示例1

输入

abcdefg
BCDEFGH

输出

BCDEFGH
abcdefg
#include<iostream>
#include<string>
using namespace std;

void Enpt(string s){
    for(int i=0;i<s.size();i++){
        if(s[i] >='a' && s[i]<='z'){
            s[i] = (s[i]-'a'+1)%26+'A';
        }
        else if(s[i]>='A' && s[i]<='Z'){
            s[i] =(s[i]-'A'+1)%26+'a';
        }
        else if(s[i]>='0' && s[i]<='9'){
            s[i]= (s[i]-'0'+1)%10+'0';
        }
    }
    cout<<s<<endl;
}

void unEnpt(string s){
    for(int i=0;i<s.size();i++){
        if(s[i] >='a' && s[i]<='z'){
            s[i] = (s[i]-'a'+25)%26+'A';
        }
        else if(s[i]>='A' && s[i]<='Z'){
            s[i] =(s[i]-'A'+25)%26+'a';
        }
        else if(s[i]>='0' && s[i]<='9'){
            s[i]= (s[i]-'0'+9)%10+'0';
        }
    }
    cout<<s<<endl;
}

int main(){

    string s1,s2;
    while(cin>>s1){
        cin>>s2;
        Enpt(s1);
        unEnpt(s2);
    }
    return 0;
}

发表于 2017-03-03 11:09:09 回复(4)
就是要注意最后的一位的变化。没有想到什么简单的方法。
#include<iostream>
#include<iomanip>
#include<string>
#include<vector>

using namespace std;

void Encrypt (string str)
{
    int len = str.size();
    for(int i=0; i<len; i++)
    {
        if(str[i]<='9'&&str[i]>='0')
        {
            if(str[i]<'9')
            {
                str[i]=str[i]+1;
            }
            else
                str[i]='0';
        }
        else if(str[i]<='z'&&str[i]>='a')
        {
            if(str[i]<'z')
            {
                str[i]=str[i]-'a'+'A'+1;
            }
            else
            {
                str[i]='A';
            }
        }
        else if(str[i]<='Z'&&str[i]>='A')
        {
            if(str[i]<'Z')
            {
                str[i]=str[i]-'A'+'a'+1;
            }
            else
            {
                str[i]='a';
            }
        }
    }
    cout<<str<<endl;
}

int unEncrypt(string str)
{
    int len = str.size();
    for(int i=0; i<len; i++)
    {
        if(str[i]<='9'&&str[i]>='0')
        {
            if(str[i]>'0')
            {
                str[i]=str[i]-1;
            }
            else
                str[i]='9';
        }
        else if(str[i]<='z'&&str[i]>='a')
        {
            if(str[i]>'a')
            {
                str[i]=str[i]-'a'+'A'-1;
            }
            else
            {
                str[i]='Z';
            }
        }
        else if(str[i]<='Z'&&str[i]>='A')
        {
            if(str[i]>'A')
            {
                str[i]=str[i]-'A'+'a'-1;
            }
            else
            {
                str[i]='z';
            }
        }
    }
    cout<<str<<endl;
    return 0;
}
int main()
{
    string str1;
    string str2;
    while(getline(cin,str1))
    {
        getline(cin,str2);
        Encrypt(str1);
        unEncrypt(str2);
    }
    return 0;
} 


发表于 2016-05-03 16:58:25 回复(13)
inttab = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
outtab = "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890"
while True:
    try:
        helper1 = input()
        s = ''
        for i in helper1:
            if i in inttab:
                s += outtab[inttab.index(i)]
            else:
                s += str(i)
        helper2 = input()
        out = ''
        for j in helper2:
            if j in outtab:
                out += inttab[outtab.index(j)]
        print(s)
        print(out)
    except:
        break
发表于 2021-09-09 21:54:43 回复(0)
#include<iostream>
#include<string>
using namespace std;
const string helper1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const string helper2 = "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890";
int main() {
	string str1;
	string str2;
	while (getline(cin, str1)) {
		getline(cin, str2);
		string res1;
		string res2;
		for (int i = 0; i < str1.size(); i++) {
			res1 += (helper2[helper1.find(str1[i])]);
		}
		for (int i = 0; i < str2.size(); i++) {
			res2 += (helper1[helper2.find(str2[i])]);
		}
		cout << res1 << endl;
		cout << res2 << endl;
	}
	return 0;
}

发表于 2017-03-15 22:14:03 回复(3)
s1 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
s2 = "1234567890BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza"
d1 = dict(zip(s1,s2))
d2 = dict(zip(s2,s1))
str1, str2 = input(), input()
for i in str1:
    print(d1[i],end="")
print()
for j in str2:
    print(d2[j],end='')

发表于 2022-07-17 23:59:13 回复(0)
python3 步步为营 ord() chr() isalpha() isdigit()
while True:
    try:
        encode = input()
        decode = input()
        length_encode, length_decode = len(encode), len(decode)
        output_encode, output_decode = "", ""
        
        # encode
        for i in range(length_encode):
            temp = encode[i]    
            if encode[i].isalpha():
                if ord("a")<=ord(temp)<ord("z"):
                    temp = (chr(ord(temp)+1)).upper()
                elif ord(temp) == ord("z"):
                    temp = "A"
                elif ord("A")<=ord(temp)<ord("Z"):
                    temp = (chr(ord(temp)+1)).lower()
                elif ord(temp) == ord("Z"):
                    temp = "a"
            elif temp.isdigit():
                temp = str((int(encode[i])+1)%10)
            output_encode = output_encode + temp
        print(output_encode)
        
        # decode
        for i in range(length_decode):
            temp = decode[i]
            if decode[i].isalpha():
                if ord("a")<ord(temp)<=ord("z"):
                    temp = (chr(ord(temp)-1)).upper()
                elif temp == "a":
                    temp = "Z"
                elif ord("A")<ord(temp)<=ord("Z"):
                    temp = (chr(ord(temp)-1)).lower()
                elif temp == "A":
                    temp = "z"
            elif decode[i].isdigit():
                temp = str((int(temp)+9)%10)
            output_decode = output_decode + temp
        print(output_decode)
        
    except:
        break



发表于 2022-07-12 15:37:02 回复(0)
#非常暴力的算法
l1 = ['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',
      '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',
      '0','1','2','3','4','5','6','7','8','9']
l2 = ['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','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','a',
      '1','2','3','4','5','6','7','8','9','0']
jia = input()
jia1 = []

jie = input()
jie1 = []

for i in jia :
    if i in l1 :
        jia1.append(l2[l1.index(i)])
for i in jia1 :
    print(i,end = '')
print()

for i in jie :
    if i in l2 :
        jie1.append(l1[l2.index(i)])
for i in jie1 :
    print(i,end = '')

发表于 2022-06-30 20:04:15 回复(0)
这个题没有什么太大的难度,就是把字符列出来,注意加解密的边境即可。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String plainText = sc.nextLine(); // 明文
            String cipherText = sc.nextLine(); // 密文
            encryptStr(plainText); // 加密
            System.out.println();
            decryptStr(cipherText); // 解密
        }
    }

    // 加密字符串
    public static void encryptStr(String plainText) {
        for (int i = 0; i < plainText.length(); i++) {
            char ch = plainText.charAt(i);
            if (ch >= 'a' && ch < 'z') {
                ch = (char)(ch + 1 - 32); // +1并转为大写
            } else if (ch >= 'A' && ch < 'Z') {
                ch = (char)(ch + 1 + 32); // +1并转为小写
            } else if (ch >= '0' && ch < '9') {
                ch = (char)(ch + 1); // +1
            } else if (ch == 'z') {
                ch = 'A'; // 小写z ==> A
            } else if (ch == 'Z') {
                ch = 'a'; // 大写Z ==> Z
            } else if (ch == '9') {
                ch = '0'; // 9 ==> 0
            }
            System.out.print(ch);
        }
    }

    // 解密字符串
    public static void decryptStr(String cipherText) {
        for (int i = 0; i < cipherText.length(); i++) {
            char ch = cipherText.charAt(i);
            if (ch > 'a' && ch <= 'z') {
                ch = (char)(ch - 1 - 32); // -1并转为大写
            } else if (ch > 'A' && ch <= 'Z') {
                ch = (char)(ch - 1 + 32); // -1并转为小写
            } else if (ch > '0' && ch <= '9') {
                ch = (char)(ch - 1); // -1
            } else if (ch == 'a') {
                ch = 'Z'; // 小写a ==> Z
            } else if (ch == 'A') {
                ch = 'z'; // 大写A ==> z
            } else if (ch == '0') {
                ch = '9'; // 0 ==> 9
            }
            System.out.print(ch);
        }
    }
}


发表于 2022-05-01 21:47:20 回复(0)
#include<stdio.h>
#include<string.h>

int main(){
    void encrypt(); //声明加密函数
    void decrypt(); //声明解密函数
    
    char str1[1001]={'\0'};
    char str2[1001]={'\0'};
    scanf("%s",str1);
    scanf("%s",str2);
    encrypt(str1);
    decrypt(str2); 
}



void encrypt(char str[1001]){
    int len = strlen(str);
    for(int i=0;i<len;i++){
        if(str[i]>='a'&&str[i]<='y')
            printf("%c",str[i]-32+1);
        else if(str[i]>='A'&&str[i]<='Y')
            printf("%c",str[i]+32+1);
        else if(str[i]=='z')
            printf("A");
        else if(str[i]=='Z')
            printf("a");
        else if(str[i]>='0'&&str[i]<='8')
            printf("%c",str[i]+1);
        else if(str[i]=='9')
            printf("0");
    }printf("\n");
}

void decrypt(char str[1001]){
    int len = strlen(str);
    for(int i=0;i<len;i++){
        if(str[i]>='b'&&str[i]<='z')
            printf("%c",str[i]-32-1);
        else if(str[i]>='B'&&str[i]<='Z')
            printf("%c",str[i]+32-1);
        else if(str[i]=='A')
            printf("z");
        else if(str[i]=='a')
            printf("Z");
        else if(str[i]>='1'&&str[i]<='9')
            printf("%c",str[i]-1);
        else if(str[i]=='0')
            printf("9");
    }printf("\n");
}

发表于 2022-04-19 16:40:13 回复(0)
为什么B加了31是Z?
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string a,b;
    cin>>a;
    cin>>b;
    for(int i=0;i<a.length();i++)
    {
        if(a[i]>=65&&a[i]<=90)
        if(a[i]!=90)
            a[i]=a[i]+33;
        else a[i]=97;
        if(a[i]>=97&&a[i]<=122)
        if(a[i]!=122)
            a[i]=a[i]-31;
        else a[i]=65;
        if(a[i]>=48&&a[i]<=57)
        if(a[i]!=57)
            a[i]=a[i]+1;
        else a[i]=48;
    }
    for(int i=0;i<b.length();i++)
    {
        if(b[i]>=65&&b[i]<=90)
        if(b[i]!=65)
            b[i]=b[i]+31;
        else b[i]=122;
        if(b[i]>=97&&b[i]<=122)
        if(b[i]!=97)
            b[i]=b[i]-33;
        else b[i]=90;
        if(b[i]>=48&&b[i]<=57)
        if(b[i]!=48)
            b[i]=b[i]-1;
        else b[i]=57;
    }
    cout<<a<<endl<<b;
}
发表于 2022-03-27 08:16:22 回复(0)
#include<iostream>
#include<string>

using namespace std;
string encryp_str(string str){//加密
    for(int i = 0;i < str.size();i++){
        if(str[i]<='Z'&&str[i]>='A'){
            str[i] = str[i] - 'A' +'a';
            str[i] = (str[i] + 1 - 'a')%26 + 'a';
        }
        else if(str[i]<='z'&&str[i]>='a'){
            str[i] = str[i] - 'a' +'A';
            str[i] = (str[i] + 1 - 'A')%26 + 'A';
        }
        else if(str[i]>='0'&&str[i]<='9'){
            str[i] = (str[i] + 1 - '0')%10 + '0';
        }
    }
    return str;
}
string deciph_str(string str){//解密
    for(int i = 0;i < str.size();i++){
        if(str[i]<='Z'&&str[i]>='A'){
            str[i] = str[i] - 'A' +'a';
            str[i] = (str[i] - 1 - 'a' + 26 ) % 26 + 'a';
        }
        else if(str[i]<='z'&&str[i]>='a'){
            str[i] = str[i] - 'a' +'A';
            str[i] = (str[i] - 1 - 'A' + 26 ) % 26 + 'A';
        }
        else if(str[i]>='0'&&str[i]<='9'){
            str[i] = (str[i] - 1 - '0' + 10 ) % 10 + '0';
        }
    }
    return str;
}
int main(){
    string str1;
    string str2;
    while(cin>>str1){
        cin>>str2;
        cout<< encryp_str(str1)<<endl;
        cout<< deciph_str(str2)<<endl;
    }
}


发表于 2022-02-11 17:39:29 回复(0)
逻辑很简单很好理解的代码
while True:
    try:
        mingwen = input()
        miwen = input()
        res1,res2 = "",""
        for i in mingwen:
            if i.isdigit():
                if i != "9":
                    res1 += str(int(i) + 1)
                else:
                    res1 += "0"
            if i.isalpha():
                if i.lower() != "z":
                    if i.islower():
                        res1 += chr(ord(i.upper())+1)
                    else:
                        res1 += chr(ord(i.lower())+1)
                else:
                    if i.islower():
                        res1 += "A"
                    else:
                        res1 += "a"
        print(res1)
        for i in miwen:
            if i.isdigit():
                if i != "0":
                    res2 += str(int(i) - 1)
                else:
                    res2 += "9"
            if i.isalpha():
                if i.lower() != "a":
                    if i.islower():
                        res2 += chr(ord(i.upper())-1)
                    else:
                        res2 += chr(ord(i.lower())-1)
                else:
                    if i.islower():
                        res2 += "Z"
                    else:
                        res2 += "z"
        print(res2)
    except:
        break


发表于 2022-01-05 09:35:31 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            System.out.println(encode(sc.nextLine(), true));
            System.out.println(encode(sc.nextLine(), false));
        }
    }

    public static String encode(String str, boolean flag) {
        String result = "";
        for(char c : str.toCharArray()) {
            // 数字
            if (c >= '0' && c <='9') {
                if (c == '9' && flag) {
                    result += '0';
                } else if (c == '0' && !flag){
                    result += '9';
                } else {
                    if (flag) {
                        result += (char)(c+1);
                    } else {
                        result += (char)(c-1);
                    }
                }
                continue;
            }
            // 小写字母
            if (c >= 'a' && c <= 'z') {
                if (c == 'z' && flag) {
                    result += 'A';
                } else if (c == 'a' && !flag) {
                    result += 'Z';
                }else {
                    if (flag) {
                        result += (char)(c-31);
                    } else {
                        result += (char)(c-33);
                    }
                }
                continue;
            }
            // 大写字母
            if (c >= 'A' && c <= 'Z') {
                if (c == 'Z' && flag) {
                    result += 'a';
                } else if (c == 'A' || !flag) {
                    result += 'z';
                }else {
                    if (flag) {
                        result += (char)(c+33);
                    } else {
                        result += (char)(c+31);
                    }
                }
                continue;
            }
            result += c;
        }
        return result;
    }
}

发表于 2021-11-07 15:07:34 回复(1)
import java.util.Scanner;

/**
 * @author Yuliang.Lee
 * @version 1.0
 * @date 2021/9/6 12:08
 * 字符串加解密:
    1、对输入的字符串进行加解密,并输出。
        第一行输入是要加密的密码,第二行输入是加过密的密码
    2、加密方法为:
        当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;
        当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;
        其他字符不做变化。
    3、解密方法为加密的逆过程。
 * 示例:
   输入
    abcdefg
    BCDEFGH
   输出
    BCDEFGH
    abcdefg
 */
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String plaintext = in.nextLine();
            String ciphertext = in.nextLine();
            for (char e : encrypt(plaintext)) {
                System.out.print(e);
            }
            System.out.println();
            for (char d : decrypt(ciphertext)) {
                System.out.print(d);
            }
            System.out.println();
        }
    }

    public static char[] encrypt(String input) {
        char[] encryption = new char[input.length()];
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                if (ch == 'Z') {
                    ch = 'a';
                }
                else {
                    ch = (char) (ch + 33);
                }
            } else if (ch >= 'a' && ch <= 'z') {
                if (ch == 'z') {
                    ch = 'A';
                }
                else {
                    ch = (char) (ch - 31);
                }
            } else if (ch >= '0' && ch <= '9') {
                if (ch == '9') {
                    ch = '0';
                }
                else {
                    ch = (char) (ch+1);
                }
            }
            encryption[i] = ch;
        }
        return encryption;
    }

    public static char[] decrypt(String input) {
        char[] decryption = new char[input.length()];
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                if (ch == 'A') {
                    ch = 'z';
                }
                else {
                    ch = (char) (ch + 31);
                }
            } else if (ch >= 'a' && ch <= 'z') {
                if (ch == 'a') {
                    ch = 'Z';
                }
                else {
                    ch = (char) (ch - 33);
                }
            } else if (ch >= '0' && ch <= '9') {
                if (ch == '0') {
                    ch = '9';
                }
                else {
                    ch = (char) (ch-1);
                }
            }
            decryption[i] = ch;
        }
        return decryption;
    }
}

发表于 2021-09-07 10:08:24 回复(0)
Java 模拟
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String plaintext = sc.nextLine();
            String pwd = sc.nextLine();
            
            System.out.println(encrpt(plaintext));
            System.out.println(decrpt(pwd));
        }
    }
    
    public static String decrpt(String pwd){
        char[] arr = pwd.toCharArray();
        
        for(int i = 0 ; i < arr.length ; i++){
            if(arr[i] >= '0' && arr[i] <= '9'){
                if(arr[i] == '0'){
                    arr[i] = '9';
                }else{
                    arr[i] = (char)(arr[i] - 1 );
                }
                continue;
            }
            
            if(arr[i] >= 'a' && arr[i] <= 'z'){
                if(arr[i] == 'a'){
                    arr[i] = 'Z';
                }else{
                    arr[i] = (char)(arr[i] - 33);
                }
                continue;
            }
            
            if(arr[i] >= 'A' && arr[i] <= 'Z'){
                if(arr[i] == 'A'){
                    arr[i] = 'z';
                }else{
                    arr[i] = (char)(arr[i] + 31);
                }
                continue;
            }
        }
        
        return String.valueOf(arr);
    }
    
    public static String encrpt(String plaintext){
        char[] arr = plaintext.toCharArray();
        
        for(int i = 0 ; i < arr.length ; i++){
            if(arr[i] >= '0' && arr[i] <= '9'){
                if(arr[i] == '9'){
                    arr[i] = '0';
                }else{
                    arr[i] = (char)(arr[i] + 1);
                }
                continue;
            }
            
            if(arr[i] >= 'a' && arr[i] <= 'z'){
                if(arr[i] == 'z'){
                    arr[i] = 'A';
                }else{
                    Character.toUpperCase(arr[i]);
                    arr[i] = (char)(arr[i] - 31);
                }
                continue;
            }
            
            if(arr[i] >= 'A' && arr[i] <= 'Z'){
                if(arr[i] == 'Z'){
                    arr[i] = 'a';
                }else{
                    Character.toLowerCase(arr[i]);
                    arr[i] = (char)(arr[i] + 33);
                }
                continue;
            }
        }
        
        return String.valueOf(arr);
    }
}


发表于 2021-08-11 01:33:19 回复(0)
#include<iostream>

using namespace std;

string Encode(string A){
    for(int i=0;i<A.size();i++){
        if(isalpha(A[i])){
            if(islower(A[i])){
                if(A[i]=='z') A[i]='A';
                else A[i]=toupper(A[i])+1;
            }
            else{
                if(A[i]=='Z') A[i]='a';
                else A[i]=tolower(A[i])+1;
            }
        }
        else if(isdigit(A[i])){
            if(A[i]=='9') A[i]='0';
            else A[i]++;
        }
    }
    return A;
}

string Decode(string A){
    for(int i=0;i<A.size();i++){
        if(isalpha(A[i])){
            if(islower(A[i])){
                if(A[i]=='a') A[i]='Z';
                else A[i]=toupper(A[i])-1;
            }
            else{
                if(A[i]=='A') A[i]='z';
                else A[i]=tolower(A[i])-1;
            }
        }
        else if(isdigit(A[i])){
            if(A[i]=='0') A[i]='9';
            else A[i]--;
        }
    }
    return A;
}

int main(){
    string forEncode, forDecode;
    while(cin>>forEncode>>forDecode){
        cout<<Encode(forEncode)<<endl;
        cout<<Decode(forDecode)<<endl;
    }
}

发表于 2021-07-28 04:47:40 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char str[500] = {0};
    char str_out[500] = {0};
    char str1[500] = {0};
    
    while(gets(str))
    {
        gets(str1);
        memset(str_out, '\0',sizeof(str_out));
        for(int i = 0; str[i] != '\0'; i++)
        {
            if(str[i] >= 'a' && str[i] <= 'z')
            {
                str_out[i] = (str[i]%'a'+1)%26+'A';
            }
            else if(str[i] >= 'A' && str[i] <= 'Z')
            {
                str_out[i] = (str[i]%'A'+1)%26+'a';
            }
            else if(str[i] >= '0' && str[i] <= '9')
            {
                str_out[i] = (str[i]%'0'+1)%10+'0';
            }
        }
        printf("%s\n", str_out);
        memset(str_out, '\0',sizeof(str_out));
        for(int i = 0; str1[i] != '\0'; i++)
        {
            if(str1[i] >= 'a' && str1[i] <= 'z')
            {
                str_out[i] = (str1[i]%'a'-1+26)%26 +'A';
            }
            else if(str1[i] >= 'A' && str1[i] <= 'Z')
            {
                str_out[i] = (str1[i]%'A'-1+26)%26 +'a';
            }
            else if(str1[i] >= '0' && str1[i] <= '9')
            {
                str_out[i] = (str1[i]%'0'-1+10)%10 +'0';
            }
        }
        printf("%s\n", str_out);
    }
}

发表于 2021-07-17 15:20:00 回复(0)
过是过了,但是运行效率不高🤣,不知道大佬们都是怎么优化的
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<string.h>
using namespace std;
void encrypt(string& s) {
    //加密
    int len=s.size();
    for(int i=0;i<len;++i) {
        if(s[i]>='a' && s[i]<='z') {
            //小写字母
            s[i]=('A'+(s[i]-'a'+1)%26); //转为后一个,且大小写转换
        } else if(s[i]>='A' && s[i]<='Z') {
            s[i]=('a'+(s[i]-'A'+1)%26);
        } else {
            //数字
            s[i]=('0'+(s[i]-'0'+1)%10);
        }
    }
}
void decrypt(string& s) {
    //解密
    int len=s.size();
    for(int i=0;i<len;++i) {
        if(s[i]>='a' && s[i]<='z') {
            //小写字母
            s[i]=('A'+(s[i]-'a'+25)%26); //转为后一个,且大小写转换
        } else if(s[i]>='A' && s[i]<='Z') {
            s[i]=('a'+(s[i]-'A'+25)%26);
        } else {
            //数字
            s[i]=('0'+(s[i]-'0'+9)%10);
        }
    }
}
int main()
{
    string a;
    while(cin>>a) {
        encrypt(a); //对a加密
        cout<<a<<endl;
        string b;
        cin>>b;
        decrypt(b); //对b解密
        cout<<b<<endl;
    }
    
    return 0;
}
发表于 2021-06-24 10:06:26 回复(0)
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        String code1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        String code2 = "bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA1234567890";
        while (cin.hasNext()) {
            String encode = cin.next();
            String decode = cin.next();
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            for (int i = 0; i < encode.length(); i++) {
                sb1.append(code2.charAt(code1.indexOf(encode.charAt(i))));
            }
            for (int i = 0; i < decode.length(); i++) {
                sb2.append(code1.charAt(code2.indexOf(decode.charAt(i))));
            }
            System.out.println(sb1);
            System.out.println(sb2);
        }
    }
}

发表于 2021-04-07 15:21:32 回复(0)
直接按照题意进行加密解密就可以了,在java中可以直接对字符进行加减法来进行转换。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String code1;
        while((code1 = br.readLine()) != null){
            code1 = code1.trim();
            // 加密
            StringBuilder encode = new StringBuilder();
            for(int i = 0; i < code1.length(); i++){
                if(code1.charAt(i) >= 'a' && code1.charAt(i) <= 'z'){
                    if(code1.charAt(i) != 'z')
                        encode.append((char)(code1.charAt(i) + 1 - 32));
                    else
                        encode.append('A');
                }else if(code1.charAt(i) >= 'A' && code1.charAt(i) <= 'Z'){
                    if(code1.charAt(i) != 'Z')
                        encode.append((char)(code1.charAt(i) + 1 + 32));
                    else
                        encode.append('a');
                }else if(code1.charAt(i) >= '0' && code1.charAt(i) <= '9'){
                    if(code1.charAt(i) != '9')
                        encode.append((char)(code1.charAt(i) + 1));
                    else
                        encode.append('0');
                }else
                    encode.append(code1.charAt(i));
            }
            String code2 = br.readLine().trim();
            // 解密
            StringBuilder decode = new StringBuilder();
            for(int i = 0; i < code2.length(); i++){
                if(code2.charAt(i) >= 'a' && code2.charAt(i) <= 'z'){
                    if(code2.charAt(i) != 'a')
                        decode.append((char)(code2.charAt(i) - 1 - 32));
                    else
                        decode.append('Z');
                }else if(code2.charAt(i) >= 'A' && code2.charAt(i) <= 'Z'){
                    if(code2.charAt(i) != 'A')
                        decode.append((char)(code2.charAt(i) - 1 + 32));
                    else
                        decode.append('z');
                }else if(code2.charAt(i) >= '0' && code2.charAt(i) <= '9'){
                    if(code2.charAt(i) != '0')
                        decode.append((char)(code2.charAt(i) - 1));
                    else
                        decode.append('9');
                }else
                    decode.append(code2.charAt(i));
            }
            System.out.println(encode);
            System.out.println(decode);
        }
    }
}

发表于 2021-03-27 14:57:35 回复(0)