我正在开发一个使用密码的程序。我需要使用的密码是 qwerty 的字母表。所以...
abcdefghijklmnopqrstuvwxyz
qwertyuiopasdfghjklzxcvbnm
程序需要获取编码 key
qwertyuiopasdfghjklzxcvbnm
并产生解码 key 。
我该如何去做呢?我过去只做过凯撒密码。
最佳答案
以下是将字符串输入转换为 qwerty 密码的 C 代码,假设您仅使用小写字母,并且对字符串使用大小为 500 的缓冲区:
#include <stdio.h>
#include <string.h>
int main() {
char* ciphertext = "qwertyuiopasdfghjklzxcvbnm"; // cipher lookup
char input[500]; // input buffer
printf("Enter text: ");
fgets(input, sizeof(input), stdin); // safe input from user
input[strlen(input) - 1] = 0; // remove the \n (newline)
int count = strlen(input); // get the string length
char output[count]; // output string
for(int i = 0; i < count; i++) { // loop through characters in input
int index = ((int) input[i]) - 97; // get the index in the cipher by subtracting 'a' (97) from the current character
if(index < 0) {
output[i] = ' '; // if index < 0, put a space to account for spaces
}
else {
output[i] = ciphertext[index]; // else, assign the output[i] to the ciphertext[index]
}
}
output[count] = 0; // null-terminate the string
printf("output: %s\n", output); // output the result
}
关于c - 将密码字母替换为 QWERTY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43522250/