Atbash 密码程序 A = Z, B = Y, C = X
Atbash Cipher Program A = Z, B = Y, C = X
>我正在尝试创建一个将生成Atbash密码的程序。基于此代码,如何反转每个字母?比如 A = Z、B = Y、C = X?
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
string encrypt(string str);
int main(){
string text = "", result = "";
cout << "Enter text to encrypt: ";getline(cin, text);
result = encrypt(text);
cout << "Encrypted text: " << result;
}
string encrypt(string str) {
string temp = str;
string sample;
char alphabeta[13] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'};
char alphabetb[13] = {'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char Ualphabeta[13];
char Ualphabetb[13];
for(int i = 0;i < 13;i++){
Ualphabeta[i] = toupper(alphabeta[i]);
}
for(int i = 0;i < 13;i++){
Ualphabetb[i] = toupper(alphabetb[i]);
}
cout << endl;
return temp;
}
不需要查找数组,因为您可以简单地计算与字母表末尾的距离,然后从中计算出来。看:
#include <iostream>
#include <string>
#include <cctype>
int main()
{
const int letterCount = 'z' - 'a' + 1 ;
std::string name = "Hello world!";
for(auto& letter: name){
if(!std::isalpha(letter)) {
continue;
}
const bool isUpper = std::isupper(letter);
const char baseOffset = isUpper? 'A': 'a';
const auto distanceFromAlphabetStart = letter - baseOffset;
letter = (baseOffset + letterCount - 1) - distanceFromAlphabetStart;
// substract the initial position from z (either uppercase or lowercase).
}
std::cout << name;
}
https://repl.it/repls/ThornyPowerfulHexadecimal
相关文章:
- 密码登录程序将永远循环并显示不正确的结果
- 如何在C++的凯撒密码程序中包含空格?
- 即使密码/用户无效,程序仍会继续
- 程序输入密码并解锁窗口7,8,10
- 凯撒密码程序中"Debug Assertion Failed!"问题
- Atbash 密码程序 A = Z, B = Y, C = X
- 密码验证程序
- C++应用程序连接到网站服务器并根据密码检索文件
- 如何制作一个从文件中读取密码并与用户编写的密码进行比较的 c++ 程序
- 简单的密码哈希程序 c++
- 在我的C++凯撒密码程序中出现错误
- 凯撒密码程序
- C++ 密码程序上的计数循环
- 密码程序在 C++ 中不起作用
- C++密码程序,字符串在后排时不会删除最后一个字符
- 密码程序来掩盖字符 - 怎么了?
- 密码程序
- c++凯撒密码程序执行错误字符
- 我的密码程序不更改密码
- c++密码程序