根据用户提供的键替换输入。现在它编译得很好并且也做了它应该做的事情

Substituting an input according to a key provided by the user. Now it compiles fine and does what it's supposed to do as well

它应该执行以下操作

  1. 请求输入:堆栈(例如)
  2. 请求密钥:qwertyuioplkjhgfdsazxcvbnm(例如)
  3. 转换成密文:Azqel

这是新的更新代码:

//Encrypting user input by using 26 character long alphabetic key provided by user
#include <stdio.h>
#include <string.h>
int main(){
char key[26];
char text[1000];
//Prompt user for key
do{
    printf("Provide a 26 character long key : ");
    scanf("%s", key);
    //If key is empty, exit code
    if(strlen(key) == 0){
        printf("Error: Empty input");
        return 1;
    }
    //If key is incomplete, prompt user again for complete key
    if(strlen(key)!= 26){
        printf("Error: Incomplete key\n");
    }
}while(strlen(key) != 26);
//If key has values other than alphabets, exit code
for(int i= 0, n= strlen(key); i<n; i++){
    if((key[i]< 'a' || key[i]> 'z') && (key[i]< 'A' || key[i]> 'Z')){
        printf("Error: Invalid key");
        return 2;
    }
}
//If key has repeated values, exit code
for(int i= 0; i< strlen(key); i++){
    for(int j= i+ 1; key[j]!= '[=10=]'; j++){
        int x, y;
        if(islower(key[i])){
            x = key[i] - 'a';
        }
        else {
            x = key[i] - 'A';
        }
        if(islower(key[j])){
            y = key[j] - 'a';
        }
        else {
            y = key[j] - 'A';
        }
    if(x == y){
    printf("Error: Repeated characters in key");
    return 3;
    }
    }
}
//Prompt user for input
printf("Plaintext : ");
scanf("%s", text);
//If input is empty, exit code
if(strlen(text) == 0){
    printf("Error: Empty input");
    return 4;
}
printf("Ciphertext: ");
for(int i= 0, n= strlen(text); i< n; i++){
    //Encrypting small letters
    if(text[i] >= 'a' && text[i] <= 'z'){
         printf("%c", key[text[i]-'a']);
    }
    //Encrypting capital letters
    else if(text[i] >= 'A' && text[i] <= 'Z'){
         printf("%c", key[text[i]-'A']-('a'-'A'));
    }
    //Printing characters other than alphabets
    else{
        printf("%c", text[i]);
    }
}
return 0;
}

代码以不同方式处理小写字符和大写字符(即使它们相同)的错误现已修复。 例如,如果密钥是:qwertyuioplkjhgfdsazxcvbmM(它有字母 'm' 和 'M' 但它们被视为不同的字符)

你可以试试这个:

#include <stdio.h>
#include <string.h>

int main(){
   char s[1000];
   char t[26];
   printf("Text: ");
   scanf("%s", s);
   printf("Key: ");
   scanf("%s", t);
   for(int i= 0, n= strlen(s); i< n; i++){
      if(s[i] >= 'a' && s[i] <= 'z') printf("%c", t[s[i]-'a']);
      else printf("%c", t[s[i]-'A'] + ('A'-'a'));
   }
   return 0;
}

并且如评论中所述,尽量不要使用 97122 等数字,而是使用 'a''z'

并且对于更新代码中提到的错误,而不是像 key[j] == key[i] 这样的简单检查,而是这样做

int x, y;
if(islower(key[i])) x = key[i] - 'a';
else x = key[i] - 'A';
if(islower(key[j])) y = key[j] - 'a';
else y = key[j] - 'A';

if(x == y){
    printf("Error: Repeated characters in key");
    return 3;
}

或者你可以用一个bool check[26],遍历key,让一个字符的值为trueif(islower(key[i])) check[key[i]-'a'] = true; else check[key[i]-'A'] = true;最后检查整个bool数组是否为true