题解 | #链表内指定区间反转#

字符串加解密

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

一、利用相应的STL库函数判断,来进行加解密。这里需要注意如下几点:

  1. 加解密函数的参数需要写成引用,这样才能修改string的值。
  2. 对于数字的判断,需要注意这里不是 c = (c + 1) % 10, 而是c = (c - '0' + 1) % 10 + '0',因为这里的c是字符,不是数字。
#include <bits/stdc++.h>
using namespace std;
int encrypt_str(string &str) {/* the params should be reference to ensure that str can be changed.*/ 
    for(auto &c:str) {
        if(isalpha(c)) {
            if(tolower(c) == 'z') {
                if(islower(c))
                    c = 'A';
                else 
                    c = 'a';
            } else {
                c = c + 1;
                if(islower(c))
                    c = toupper(c);
                else 
                    c = tolower(c);
            } // else 
        } else if(isdigit(c)) {
            c = (c - '0' + 1) % 10 + '0';
        } else {
            continue;
        }
    }
    return 0;
}

int decrypt_str(string &str) {
    for(auto &c:str) {
        if(isalpha(c)) {
            if(tolower(c) == 'a') {
                if(islower(c)) {
                    c = 'Z';
                } else {
                    c = 'z';
                }
            } else {
                c = c - 1;
                if(islower(c)) {
                    c = toupper(c);
                } else  {
                    c = tolower(c);
                } // if(islower(c))
            }// if(isalpha(c))
        } else if(isdigit(c)){
             c = (c - '0' - 1 + 10) % 10 + '0';
        } else {
            continue;
        } // if (alpha(c))
    }// for
    return 0;
}

int main()
{
    string to_encrypt;
    string to_decrypt;
    cin >> to_encrypt >> to_decrypt;
    encrypt_str(to_encrypt);
    decrypt_str(to_decrypt);
    cout << to_encrypt << endl;
    cout << to_decrypt << endl;
    return 0;
}

二、打表法

#include <bits/stdc++.h>
using namespace std;
int encrypt_str(string &str) {
   static const string encrpyt_table = "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890";
	for(auto &c:str) {
    	if(isalpha(c)) {
            if(islower(c)) {
                c = encrpyt_table[c -'a'];
            } else {
                c = encrpyt_table[c -'A' + 26];
            }
        	
        } else if(isdigit(c)) {
        	c = encrpyt_table[c -'0' + 52];
        }
    }
  return 0;
}

int decrypt_str(string &str) {
  static const string decrpyt_table = "ZABCDEFGHIJKLMNOPQRSTUVWXYzabcdefghijklmnopqrstuvwxy9012345678";
  for(auto &c:str) {
    	if(isalpha(c)) {
        	if(islower(c)) {
                c = decrpyt_table[c -'a'];
            } else {
                c = decrpyt_table[c -'A' + 26];
            }
        } else if(isdigit(c)) {
        	c = decrpyt_table[c -'0' + 52];
        }
    } //for
  return 0;
}

int main()
{
    string to_encrypt;
    string to_decrypt;
    cin >> to_encrypt >> to_decrypt;
    encrypt_str(to_encrypt);
    decrypt_str(to_decrypt);
    cout << to_encrypt << endl;
    cout << to_decrypt << endl;
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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