2017-04-20 100 views
0

我正在使用密码的程序。我需要使用的密码是qwerty的字母表。所以......替代密码字母QWERTY

abcdefghijklmnopqrstuvwxyz 
qwertyuiopasdfghjklzxcvbnm 

程序需要采取编码关键

qwertyuiopasdfghjklzxcvbnm 

,并产生解码密钥。

我该怎么做呢?我以前只做过凯撒密码。

+1

常量或静态常量查找表/阵列。索引与'a'或'q'的偏移量 – ThingyWotsit

回答

0

下面是用C代码字符串输入转换为QWERTY密码,假设你只有小写字母的工作,并且使用的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 
} 
+1

幻数:97:(( – ThingyWotsit