我正在撰寫一個程式來解決一個隨機代碼。(代碼不會有大寫字母,任何形式的符號,如句號)代碼在不知道密鑰的情況下使用移位密碼,它也可以加密代碼,但我會繼續努力稍后一個。現在我想先輸出所有 25 個結果。
但是,如果我輸入諸如“背靠背”之類的內容,它會在最后輸出一個問號。
這是我的代碼
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char code[400];
char after[400]; // variable initialization
int i, x, choice, z, temp, y;
printf(" Welcome\n");
printf("Enter your code here:\n");
scanf("%[^'\n']s", code);
printf("Press 1 to Decrypt\nPress 2 to encrypt\nPress 3 to Exit\n");
scanf("%d", &choice);
// case 1 is decryption
switch (choice) {
case 1:
for (x = 1; x <= 25; x ) {
for (i = 0; (i <= 400 && code[i] != 0); i ) // for loop
{
if (code[i] == 32) { // 32 means space in ascii code
after[i] = 32;
continue; // if there is a space it will continue
}
if (code[i] x > 122) { // 122 is z in ascii code
after[i] = code[i] x; // if it is higher than 'z'
after[i] = (after[i] - 122) 96; // it start counting form 'a'
continue;
} else {
after[i] = code[i] x;
// if nothing is wrong,it starting adding the key
}
}
printf("After decrypting:%s\n", after);
printf("%s\n", code);
}
break;
// case 2 is ecnrypt so its not important
case 2:
x = 5;
for (i = 0; (i <= 400 && code[i] != 0); i ) // for loop
{
if (code[i] == ' ') {
continue;
}
if (code[i] x > 122) {
code[i] = code[i] x;
code[i] = (code[i] - 122) 96;
continue;
} else {
code[i] = code[i] x;
}
}
printf("After decrypting:%s", code);
break;
// not important as well
case 3:
printf("GOODBYE");
break;
// not important as well
default:
printf("Errors");
return 0;
}
}
uj5u.com熱心網友回復:
要解決移位密碼,您只需要一本字典和密文的前十個左右的單詞。嘗試所有可能性,并根據您的字典測驗每個“解碼”的明文單詞。選擇匹配率最高的密碼。
uj5u.com熱心網友回復:
你忘了 null terminate after。
也許你應該這樣做:
for (i = 0; (i <= 400 && code[i] != 0); i ) {
if (code[i] == 32) {
after[i] = ...
} else if (code[i] x > 122) {
after[i] = ...
} else {
after[i] = ...
}
}
after[i] = '\0'; // <-- This line is missing.
printf("After decrypting:%s\n", after);
printf("%s\n", code);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351341.html
上一篇:如何遍歷字典并根據鍵為變數賦值?
