#include <stdio.h>
#include <string.h>
#include <ctype.h>
int prepare_key(char *key) {
if (key == NULL || strlen(key) == 0) {
return 0;
}
char alphabet[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //加密比对码
char modified_key[27];
int index = 0;
for (int i = 0; i < strlen(key); i++) {
char ch = toupper(key[i]); //将读入的小写字母转换成大写
// 如果字符是大写字母且不在 modified_key 中,则添加到 modified_key 中
if (ch >= 'A' && ch <= 'Z' && strchr(modified_key, ch) == NULL) {
modified_key[index++] = ch;
}
}
// 遍历字母表,将不在 modified_key 中的字母添加到 modified_key 中
for (int i = 0; i < 26; i++) {
if (strchr(modified_key, alphabet[i]) == NULL) {
modified_key[index++] = alphabet[i];
}
}
strcpy(key, modified_key);
return 1;
}
int main() {
char key[27];
printf("请输入加密初始值:\n");
scanf("%s", key);
if (prepare_key(key)) {
printf("Prepared key:\n%s", key);
} else {
printf("Error key.\a\a\a");
}
return 0;
}
12-16